How to use ASP.net to back up and restore SQL Server databases

Source: Internet
Author: User
Tags sql net string trim backup

First, we first analyze the ASP backup SQL database code, detailed as follows:

To back up the SQL Server database:
String SqlStr1 = "server= (local);d atabase= '" + this. Dropdownlist1.selectedvalue + "'; Uid=sa; Pwd= ";
String SqlStr2 = "BACKUP Database" + this. Dropdownlist1.selectedvalue + "to disk= '" + this. TextBox1.Text.Trim () + ". Bak '";
SqlConnection con = new SqlConnection (SQLSTR1);
Con. Open ();
Try
{
If File.exists (this. TextBox1.Text.Trim ()))
{
Response.Write (' <script language=javascript>alert (' This file already exists, please enter it again! '); location= ' default.aspx ' </script> ');
Return
}
SqlCommand com = new SqlCommand (SqlStr2, con);
Com. ExecuteNonQuery ();
Response.Write (' <script language=javascript>alert (' Backup data Successful! '); location= ' default.aspx ' </script> ');
}
catch (Exception error)
{
Response.Write (Error. message);
Response.Write (' <script language=javascript>alert (' Backup data failed! ') </script> ");
}
Finally
{
Con. Close ();
}

The following is the ASP.net method for restoring the SQL database, with the following code:

To restore the SQL Server database:
String path = this. FileUpload1.PostedFile.FileName; Get backup path and database name
String dbname = this. Dropdownlist1.selectedvalue;
String SqlStr1 = "server= (local);d atabase= '" + this. Dropdownlist1.selectedvalue + "'; Uid=sa; Pwd= ";
String SqlStr2 = "Use master Restore Database" + dbname + "from disk= '" + Path + "'";
SqlConnection con = new SqlConnection (SQLSTR1);
Con. Open ();
Try
{
SqlCommand com = new SqlCommand (SqlStr2, con);
Com. ExecuteNonQuery ();
Response.Write (' <script language=javascript>alert (' Restore data success! '); location= ' default.aspx ' </script> ');
}
catch (Exception error)
{
Response.Write (Error. message);
Response.Write (' <script Language=javascript>alert (' failed to restore data! ') </script> ");
}
Finally
{
Con. Close ();
}



Related Article

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.