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 successful! '); Location = 'default. aspx '</script> "); } Catch (Exception error) { Response. Write (error. Message ); Response. Write ("<script language = javascript> alert ('data backup failed! ') </Script> "); } Finally
|
2. 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 ('data restoration failed! ') </Script> "); } Finally
|