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 ();
}