C # Use sqldmo. DLL to back up and restore the database

Source: Internet
Author: User
Sqldmo (SQL distributed management objects, SQL distributed management object) encapsulates objects in the Microsoft SQL Server database. Sqldmo is the application interface used by the enterprise manager in Microsoft SQL Server. Therefore, it can perform many functions, including database backup and recovery.

Sqldmo is provided by sqldmo. dll of Microsoft SQL Server. Because sqldmo. dll is a COM object, you must add references to it in the. NET project before using sqldmo. dll.

The following is a class written in C # for Microsoft SQL Server database backup and recovery:

 

Code
Using system;
Namespace dbservice
{
///
/// Dboper class, mainly using sqldmo to back up and restore the Microsoft SQL Server database
///
Publicsealedclass dboper
{
///
/// Dboper Constructor
///
Private dboper ()
{
}
///
/// Database Backup
///
Publicstaticvoid 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 ();
}
}
///
/// Restore the database
///
Publicstaticvoid 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 ();
}
}
}
}
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.