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