1. Add the SQLDMO DLL file reference in the project (SQLDMO (SQL distributed Management objects,sql Distributed Management Object))
2 using SQLDMO reference on corresponding page
3. The following are classes written in the C # language for backup and recovery of Microsoft SQL Server databases:
Using System;
Namespace Dbservice
{
<summary>
Dboper class, primarily for backup and recovery of Microsoft SQL Server databases
</summary>
public sealed class Dboper
{
<summary>
Dboper Constructors for class
</summary>
Private Dboper ()
{
}
<summary>
Database backup
</summary>
public static void DbBackup ()
{
Try
{
SQLDMO. Backup obackup = new SQLDMO. Backupclass ();
SQLDMO. SQL Server 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>
Database recovery
</summary>
public static void Dbrestore ()
{
Try
{
SQLDMO. Restore Orestore = new SQLDMO. Restoreclass ();
SQLDMO. SQL Server 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
}
}
}
}
See Using SQLDMO to back up and restore a Microsoft SQL server database in C #
Http://dev.csdn.net/develop/article/28/28564.shtm
The above method works when you are not using the database you want to recover, but you must kill the process when you use the database
The code is as follows:
<summary>
Restore DATABASE functions
</summary>
<param name= "strDbName" > Database name </param>
<param name= "strFileName" > The full path name of the database backup file </param>
<returns></returns>
public bool RestoreDB (string strdbname,string strFileName)
{
PBar = Pgbmain;
SQLDMO. SQL Server SVR = new SQLDMO. Sqlserverclass ();
Try
{
Server name, database user name, database user name 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 a 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 ();
}
}
C #, SQL database backup restore