C #, SQL database backup restore

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.