I have been confused about this problem for a long time. I have not found a complete solution on the Internet. It is not too simple, or I am talking about it. No one has answered this question on some forums. today, I completely solved this problem and passed the test in C. now, I want to help my friends (If You Want To reprint it, remember to give me the copyright. hey hey !!!). The following information is based on the information on the Internet and my actual problems.
Backup:
Write in the backup button:
CopyCode The Code is as follows: protected void button#click (Object sender, eventargs E)
{
String Path = "e :\\ Maz Database Backup \" + menu + ". Bak ";
If (file. exists (PATH ))
{
File. Delete (PATH); // note that this step is very important. If it is repeated, the data backed up will become,
// Delete the data you just created.
}
If (! File. exists (PATH ))
{
Filestream FS = file. Create (PATH );
FS. Close ();
}
String backupstr = "backup database test to disk = '" + path + "';";
Sqlconnection con = new sqlconnection ("Server = localhost; database = menu; uid = sa; Pwd = sa ;");
Sqlcommand cmd = new sqlcommand (backupstr, con );
Try
{
Con. open ();
Cmd. executenonquery ();
MessageBox. Show ("backup successful! ");
Connection. Close ();
}
Catch (exception ex)
{
String stringerror = ex. tostring ();
MessageBox. Show ("backup failed! ");
Connection. Close ();
}
}
Restore:
Write in the restore button:Copy codeThe Code is as follows: protected void button2_click (Object sender, eventargs E)
{
String Path = "e :\\ Maz Database Backup \" + menu + ". Bak ";
String connectionstringtest = "Server = localhost; database = Master; uid = sa; Pwd = sa ";
Sqlconnection connection = new sqlconnection (connectionstringtest );
String backupstr = "Restore database menu from disk = '" + path + "';";
Try
{
String SQL = "Exec killspid'" + menu + "'"; // This is critical. Otherwise, an error occurs.
Sqlcommand cmd = new sqlcommand (SQL, connection );
Connection. open ();
Cmd. executenonquery ();
Cmd = new sqlcommand (backupstr, connection );
Cmd. executenonquery ();
MessageBox. Show ("restored successfully! ");
Connection. Close ();
}
Catch (exception ex)
{
String stringerror = ex. tostring ();
MessageBox. Show ("recovery failed! ");
Connection. Close ();
}
}
Stored Procedure killspid copy Code the code is as follows: Create proc killspid (@ dbname varchar (20 ))
as
begin
declare @ SQL nvarchar (500)
declare @ spid int
set @ SQL = 'destare getspid cursor for
select spid from sysprocesses where dbid = db_id (''' + @ dbname +'' ') '
exec (@ SQL)
open getspid
fetch next from getspid into @ spid
while @ fetch_status <>-1
begin
exec ('kill ') + @ spid
fetch next from getspid into @ spid
end
close getspid
deallocate getspid
end