Use sqldmo in C # To back up and restore the SQL Server database)

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.