#regionClass description//-----------------------------------------------------------------------------////Project Name: * * *//file name: DBBakManager.cs//File Description: Manages the database backup file. Provides backup, restore, backup file management operations. ////Start Date: April 04, 2007//Developer: * * *////-----------------------------------------------------------------------------#endregionusingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingSQLDMO;//Add Reference C:Program filesmicrosoft SQL Server80ToolsBinnSQLDMO.dllnamespaceGSIM. stumanager{/// <summary> ///the SQL Server database backup files are managed. Provides backup, restore, backup file management operations. /// </summary> Public classDbbakmanager {field#regionFieldPrivate string_servername; Private string_username; Private string_password; Private string_dbname; #endregionConstruction#regionStructure/// <summary> ///constructor Function/// </summary> /// <param name= "ServerName" >Database server name/// <param name= "UserName" >Database user name/// <param name= "password" >User Password/// <param name= "DbName" >Database name PublicDbbakmanager (stringServerName,stringUserName,stringPasswordstringdbName) {_servername=ServerName; _username=UserName; _password=password; _dbname=DbName; } #endregionMethod#regionMethod/// <summary> ///backing up the database/// </summary> /// <param name= "Savefilename" >saved backup file path (including file name, file name format: "DBName (yyyymmddhhmm). bak")/// <returns>The operation returned true successfully, otherwise false</returns> Public BOOLBackupDB (stringsavefilename) {SQLDMO. SQL Server SVR=NewSQLDMO. Sqlserverclass (); Try{svr. Connect (_servername, _username, _password); SQLDMO. Backup Bak=NewSQLDMO. Backupclass (); Bak. Action=SQLDMO. Sqldmo_backup_type. Sqldmobackup_database; Bak. Initialize=true; Bak. Database=_dbname; Bak. Files=Savefilename; Bak. SQLBackup (SVR); return true; } Catch(Exception err) {return false; } finally{svr. Close (); } } /// <summary> ///Restoring a database backup/// </summary> /// <param name= "FileName" >restored backup file path (including file name, file name format: "DBName (yyyymmddhhmm). bak")/// <returns>The operation returned true successfully, otherwise false</returns> Public BOOLRestoreDB (stringfileName) {SQLDMO. SQL Server SVR=NewSQLDMO. Sqlserverclass (); Try{svr. Connect (_servername, _username, _password); //get a list of all processesSQLDMO. QueryResults qr = svr. EnumProcesses (-1); intIcolpidnum =-1; intIcoldbname =-1; //Find and recover database related processes for(inti =1; I <= qr. Columns; i++) { stringStrName =Qr.get_columnname (i); if(Strname.toupper (). Trim () = ="SPID") {Icolpidnum=i; } Else if(Strname.toupper (). Trim () = ="DBNAME") {Icoldbname=i; } if(Icolpidnum! =-1&& Icoldbname! =-1) Break; } //close the related process for(inti =1; I <= qr. Rows; i++) { intLpid =qr. GetColumnLong (i, icolpidnum); stringstrDbName =qr. GetColumnString (i, icoldbname); if(Strdbname.toupper () = =_dbname) SVR. KillProcess (LPID); } SQLDMO. Restore Res=NewSQLDMO. Restoreclass (); Res. Action=SQLDMO. Sqldmo_restore_type. Sqldmorestore_database; Res. Files=FileName; Res. Database=_dbname; Res. FileNumber=1; Res. ReplaceDatabase=true; Res. SQLRestore (SVR); return true; } Catch(Exception err) {return false; } finally{svr. Close (); } } /// <summary> ///get all database backup file information, such as: 200704041120. /// </summary> /// <param name= "FilePath" >the path to the folder where the backup file is saved (not including the file name)/// <returns>string group containing the file information</returns> Public string[] Getalldbbak (stringFilePath) { string[] Dbbakinfo = Directory.GetFiles (FilePath,"bmis*"); intStart = dbbakinfo[0]. IndexOf ("("); intEnd = dbbakinfo[0]. IndexOf (")"); //remove the superfluous part and return the time information in the name, such as: 200704041120. for(inti =0; i < dbbakinfo.length; i++) {Dbbakinfo[i]= Dbbakinfo[i]. Substring (Start +1, End-start-1); } returnDbbakinfo; } /// <summary> ///Delete a database backup file/// </summary> /// <param name= "FileName" >deleted backup file path (including file name, file name format: "DBName (yyyymmddhhmm). bak")/// <returns>The operation returned true successfully, otherwise false</returns> Public BOOLDeletedbbak (stringfileName) { if(File.exists (fileName) = =true) {file.delete (fileName); return true; } Else { return false; } } #endregion }} <pre></pre>