ASP. NET backup and restoration of SQL Server databases

Source: Internet
Author: User

I. Back up SQL Server databases


String SqlStr1 = "Server = (local); database ='' "+ 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 input it again! ''); Location = ''default. aspx'' </script> "); return;
}
SqlCommand com = new SqlCommand (SqlStr2, con );
Com. ExecuteNonQuery ();
Response. Write ("<script language = javascript> alert ('' data backed up successfully! ''); Location = ''default. aspx'' </script> ");
}
Catch (Exception error)
...{
Response. Write (error. Message); Response. Write ("<script language = javascript> alert ('' data backup failed! '') </Script> ");
}
Finally
...{
Con. Close ();
} II. Restore the SQL Server database


String path = this. FileUpload1.PostedFile. FileName;
// Obtain the backup path and Database Name
String dbname = this. DropDownList1.SelectedValue;
String SqlStr1 = "Server = (local); database ='' "+ 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 ('' data restored successfully! ''); 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.