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 provided by Microsoft SQL Server SQLDMO.dll, and since SQLDMO.dll is a COM object, you must add a reference to it in the. NET project before you use it
The following is a class written in the C # language for backup and recovery of Microsoft SQL Server databases:
Using System;
Namespace Dbservice
{
<summary>
Dboper class, which primarily applies sqldmo to backup and restore Microsoft SQL Server databases
</summary>
public sealed class Dboper
{
<summary>
Constructors for Dboper classes
</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
{
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 ();
}
}
}
}
Although this code is very short, but very practical, I hope to be able to help you: