Asp.net because the database is using a solution

Source: Internet
Author: User

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

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.