C #. Restore SQL database backup,
1. Add SQLDmo dll file reference to the project (SQLDMO (SQL Distributed Management Objects, SQL Distributed Management object ))
2. Add using SQLDMO reference to the corresponding page
3. The following is a class written in C # for Microsoft SQL Server database backup and recovery:
Using System;
Namespace DbService
{
/// <Summary>
/// DbOper class, mainly used 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 ()
{
Try
{
SQLDMO. Backup oBackup = new SQLDMO. BackupClass ();
SQLDMO. SQLServer oSQLServer = new SQLDMO. SQLServerClass ();
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;
}
}
/// <Summary>
/// Restore the database
/// </Summary>
Public static void DbRestore ()
{
Try
{
SQLDMO. Restore oRestore = new SQLDMO. RestoreClass ();
SQLDMO. SQLServer oSQLServer = new SQLDMO. SQLServerClass ();
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;
}
}
}
}
For more information, see use SQLDMO in C # To back up and restore a Microsoft SQL Server database.
Http://dev.csdn.net/develop/article/28/28564.shtm
The above method is feasible when you do not use the database to be restored, but when you use the database, you must kill the process.
The Code is as follows:
/// <Summary>
/// Restore database functions
/// </Summary>
/// <Param name = "strDbName"> database name </param>
/// <Param name = "strFileName"> full path name of the database backup file </param>
/// <Returns> </returns>
Public bool RestoreDB (string strDbName, string strFileName)
{
// PBar = pgbMain;
SQLDMO. svr = new SQLDMO. SQLServerClass ();
Try
{
// Server name, database username, database username and password
Svr. Connect ("localhost", "sa", "hai ");
SQLDMO. QueryResults qr = svr. EnumProcesses (-1 );
Int iColPIDNum =-1;
Int iColDbName =-1;
For (int I = 1; I <= qr. Columns; I ++)
{
String strName = qr. get_ColumnName (I );
If (strName. ToUpper (). Trim () = "SPID ")
{
IColPIDNum = I;
}
Else if (strName. ToUpper (). Trim () = "DBNAME ")
{
IColDbName = I;
}
If (iColPIDNum! =-1 & iColDbName! =-1)
Break;
}
// Kill the process that uses the strDbName Database
For (int I = 1; I <= qr. Rows; I ++)
{
Int lPID = qr. GetColumnLong (I, iColPIDNum );
String strDBName = qr. GetColumnString (I, iColDbName );
If (strDBName. ToUpper () = strDbName. ToUpper ())
{
Svr. KillProcess (lPID );
}
}
SQLDMO. Restore res = new SQLDMO. RestoreClass ();
Res. Action = 0;
Res. Files = strFileName;
Res. Database = strDbName;
Res. ReplaceDatabase = true;
Res. SQLRestore (svr );
Return true;
}
Catch
{
Return false;
}
Finally
{
Svr. DisConnect ();
}
}