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 ();
}
}
}
}