In C #, sqldmo is used to back up and restore sqldmo (SQL distributed management objects, SQL distributed management object) in Microsoft SQL Server. Sqldmo is an application used by the enterprise manager in Microsoft SQL Server. Program Interface, so it can execute a lot of functions, of course, also includes database backup and recovery.
Sqldmo is a sqldmo provided by Microsoft SQL Server. DLL provided, because sqldmo. DLL is a COM object, so before using it, you must. add a reference to it in the. NET project, as shown in:
The following is a class written in C # for Microsoft SQL Server database backup and recovery:
Using System;
Namespace Dbservice
{
/// <Summary>
/// Dboper class. It mainly uses sqldmo to back up and restore Microsoft SQL Server databases.
/// </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>
/// Database recovery
/// </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 ();
}
}
}
}
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 a sqldmo provided by Microsoft SQL Server. DLL provided, because sqldmo. DLL is a COM object, so before using it, you must. add a reference to it in the. NET project, as shown in:
The following is a class written in C # for Microsoft SQL Server database backup and recovery:
Using System;
Namespace Dbservice
{
/// <Summary>
/// Dboper class. It mainly uses sqldmo to back up and restore Microsoft SQL Server databases.
/// </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>
/// Database recovery
/// </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 ();
}
}
}
}