The last item was made, involving the restoration and recovery of the database, I found it online, using SQLDMO implementation, as long as the SQLDMO reference is just, then use the method of the following classes to be implemented.
I extend the class of the original author, you can automatically identify the database connection string in Web.config, and you can restore the recovered information by variable setting.
When you need to pay attention, the problem is the biggest problem, and some other users cannot restore when using the database, the solution is to add a stored procedure in the Master database:
create proc killspid (@dbname varchar (20)) asbegindeclare @sql nvarchar (500) declare @spid intset @ sql = 'declare getspid cursor for select spid from sysprocesses where dbid = db_id (' '' @ dbname '' ')' exec (@sql) open getspidfetch next from getspid into @spidwhile @@ fetch_status <> - 1beginexec ( 'kill' @ spid) fetch next from getspid into @spidendclose getspiddeallocate getspidendGO
Execute this stored procedure before restoring, you need to pass dbname, which is the name of your database. The following is the original code: (the database connection string in web.config is constr)
Using system;
Using system.configuration;
Using system.data.sqlclient;
Using system.data;
Namespace web.base_class
{
///
/// DBOPER class, mainly applied SQLDMO to implement backup and recovery of Microsoft SQL Server database
/// summary>
Public Class Dboper
{
PRIVATE STRING SERVER;
PRIVATE STRING UID;
Private string pwd;
PRIVATE STRING DATABASE;
PRIVATE STRING CONN;
///
/// DBOPER class constructor
/// summary>
Public dboper ()
{
CONN = system.configuration.configurationSettings.appsettings ["constr"]. Tostring ();
Server = CUT (CONN, "Server =", ";");
UID = CUT (CONN, "UID =", ";");
PWD = CUT (CONN, "PWD =", ";");
Database = CUT (CONN, "Database =", ";");
}
Public String Cut (String Str, String BG, String Ed)
{
String sub;
Sub = str.substring (Str.Indexof (BG) bg.length); Sub = Sub.Substring (0, Sub.indexof (";"));
Return Sub;
}
///
/// database backup
/// summary>
Public Bool DBBackup (String URL)
{
SqldMo.backup Obackup = new sqldmo.backupClass ();
Sqldmo.sqlserver osqlserver = new sqldmo.sqlserverclass ();
Try
{
OSQLServer.loginsecure = false;
OSQLServer.Connect (Server, UID, PWD);
Obackup.action = SqldMo.sqldMo_Backup_type.sqldmobackup_database;
Obackup.Database = Database;
OBACKUP.FILES = URL; // "D: /Northwind.bak";
Obackup.backupsetname = Database;
OBACKUP.BACKUPSETDESCRIPTION = "Database Backup";
OBACKUP.INITIALIZE = True;
Obackup.sqlbackup (OSQLServer);
Return True;
}
Catch
{
Return False;
Throw;
}
Finally
{
OSQLServer.disconnect ();
}
}
///
/// Database Recovery
/// summary>
Public String Dbrestore (String URL)
{
IF (ExEPro ()! = true) // Execute Store Procedure
{
Return "Failure";
}
Else
{
SqldMo.Restore orestore = new sqldmo.restoreClass ();
Sqldmo.sqlserver osqlserver = new sqldmo.sqlserverclass ();
Try
{
OSQLServer.loginsecure = false;
OSQLServer.Connect (Server, UID, PWD);
Ostore.Action = sqldmo.sqldmo_restore_type.sqldmoStore_Database;
orestore.database = database;
Ostore.Files = URL; @ "D: /NorthWind.bak";
orestore.filenumber = 1;
orestore.replacedatabase = true;
Ostore.sqlrestore (OSQLServer);
Return "OK";
}
Catch (Exception E)
{
Return "Restore Database Fails";
Throw;
}
Finally
{
OSQLServer.disconnect ();
}
}
}
Private bool exepro ()
{
SqlConnection conn1 = new SQLCONNECTION ("Server =" server "; uid =" uid "; pwd =" pwd "; dataBase = master"); SQLCommand cmd = new SQLCommand ("Killspid", conn1);
cmd.commandtype = commandtype.storedProcedure;
Cmd.Parameters.Add ("@ dbname", "port");
Try
{
CONN1.OPEN ();
cmd.executenonquery ();
Return True;
}
Catch (Exception EX)
{
Return False;
}
Finally
{
CONN1.CLOSE ();
}
}
}
}