Using SQLDMO to back up and restore SQL Server databases in C #

Source: Internet
Author: User
Tags microsoft sql server connect backup
server| Backup | recovery | data | database SQLDMO(SQL distributed Management objects,sql Distributed Management Objects) encapsulates objects in a Microsoft SQL Server database. SQLDMO is the application interface used by Enterprise Manager in Microsoft SQL Server, so it can perform many functions, including, of course, backup and recovery of the database.

SQLDMO is brought by Microsoft SQL Server SQLDMO.dllProvided, as SQLDMO.dllis a COMObjects (about COMThe concept of, please refer to my com and COM + technologyThat article), so everyone must be in before using. NET project, add a reference to it.

The following is a class written in the C # language for backup and recovery of Microsoft SQL Server databases:

public  sealed  class  dboper {    ///<summary>    // /Dboper class constructor///author:shadow    ///</summary>     private DbOper ()      {    }      ///<summary>     ///Database backup     ///</summary>     public  static   void DbBackup ()      {          SQLDMO. Backup obackup = new SQLDMO. Backupclass ();           SQLDMO. SQL Server 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 ();         }     }      /// <summary>    ///Database Recovery     ///</summary>      public  static  void Dbrestore ()      {           SQLDMO. Restore Orestore = new SQLDMO. Restoreclass ();           SQLDMO. SQL Server oSQLServer = new SQLDMO. Sqlserverclass ();          try         {          &NBSP;&Nbsp;   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 ();         }     }}


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.