Asp. NET backup, restore SQL Server database

Source: Internet
Author: User
Tags trim
One, backup 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= NewSqlCommand (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 (") Failed to backup data! ') </script>");
}
finally
... {
Con. Close ();
}Second, 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.