In C #, sqldmo is used to back up and restore sqldmo (SQL distributed management objects, SQL distributed management object) in SQL Server to encapsulate objects in Microsoft SQL Server database. Sqldmo is an application used by the enterprise manager in Microsoft SQL Server. Program Interface, so it can execute a lot of functions, of course, also includes database backup and recovery.
Sqldmo is provided by sqldmo. dll of Microsoft SQL Server. Because sqldmo. dll is a COM Object (for the concept of COM, see my article on COM and COM + technology ).Article), So you must add a reference to it in the. NET project before using it.
The following is a class written in C # for Microsoft SQL Server database backup and recovery:
Public Sealed Class Dboper { /// <Summary> /// Dboper Constructor /// Author: Shadow /// </Summary> Private Dboper () { } /// <Summary> /// Database Backup /// </Summary> Public Static Void Dbbackup () { Sqldmo. Backup obackup = New Sqldmo. backupclass (); Sqldmo. sqlserver osqlserver = New Sqldmo. sqlserverclass (); Try { Osqlserver. loginsecure = False ; Osqlserver. Connect ("localhost", "sa", "1234 "); Obackup. Action = sqldmo. sqldmo_backup_type.sqldmobackup_database; Obackup. Database = "northwind "; Obackup. Files = @ "D: \ northwind. Bak "; Obackup. backupsetname = "northwind "; Obackup. backupsetdescription = "database backup "; Obackup. initialize = True ; Obackup. sqlbackup (osqlserver ); } Catch { Throw ; } Finally { Osqlserver. Disconnect (); } } The statement retrieves a list of all processes:
Sqldmo. queryresults QR = SVR. enumprocesses (-1 );
The following statement finds and kills the process related to database Restoration:
Int icolpidnum =-1;
Int icoldbname =-1;
For (INT I = 1; I <= QR. columns; I ++)
{
String strname = 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;
}
For (INT I = 1; I <= QR. Rows; I ++)
{
Int LPID = QR. getcolumnlong (I, icolpidnum );
String strdbname = QR. getcolumnstring (I, icoldbname );
If (strdbname. toupper () = strdbname. toupper ())
SVR. killprocess (LPID );
}
/// <Summary> /// Database recovery /// </Summary> Public Static Void Dbrestore () { Sqldmo. Restore orestore = New Sqldmo. restoreclass (); Sqldmo. sqlserver osqlserver = New Sqldmo. sqlserverclass (); Try { Osqlserver. loginsecure = False ; Osqlserver. Connect ("localhost", "sa", "1234 "); Orestore. Action = sqldmo. sqldmo_restore_type.sqldmorestore_database; Orestore. Database = "northwind "; Orestore. Files = @ "D: \ northwind. Bak "; Orestore. filenumber = 1; Orestore. replacedatabase = True ; Orestore. sqlrestore (osqlserver ); } Catch { Throw ; } Finally { Osqlserver. Disconnect (); } } }
Register components used by sqldmo. dll
Sqldmo. dll
Sqldmo. rll
Sqlresld. DL
Sqlsvc. dll
Sqlsvc. rll
Sqlwid. dll
Sqlwoa. dll
W95scm. dll
Note: If the registration fails, update the MDAC version to 2.8.