C # back up 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.
Of course, sqldmo is used for this job.
Find the sqldmo. dll file.
C: \ Program Files \ Microsoft SQL Server \ 80 \ Tools \ Binn \ sqldmo. dll
Convert the tool provided by the. Net Framework into managed code
Tlbimp sqldmo. dll/out: c: \ SQLDMO. dll

 

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:

Using System;
Namespace DbService
{
/// <Summary>
/// DbOper class, mainly using SQLDMO to back up and restore the Microsoft SQL Server database
/// </Summary>
Public sealed class DbOper
{
/// <Summary>
/// DbOper Constructor
/// </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 ();
}
}
/// <Summary>
/// Restore the database
/// </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 ();
}
}
}
}

 

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.