This is an extremely beautiful ASP. NET database restoration and backup code. Now let's talk about the. net database restoration and backup code with examples.
This is an extremely beautifulASP. NET database restoration tutorial and backup codeWell, let's talk about the. net tutorial restoring and backing up the database code with examples below.
Html code
<Table>
<Tr>
<Td style = "width: 100px"> <span style = "font-size: 9pt"> operational data warehouse </span> </td>
<Td> <asp Tutorial: DropDownList ID = "DropDownList1" runat = "server" Font-Size = "9pt" Width = "124px"> </asp: dropDownList> </td>
<Td style = "width: 100px"> </td>
</Tr>
<Tr>
<Td style = "width: 100px"> <span style = "font-size: 9pt"> Backup name and location </span> </td>
<Td style = "width: 100px"> <asp: textBox ID = "TextBox1" runat = "server" Font-Size = "9pt" Width = "117px"> </asp: TextBox> </td>
<Td style = "width: 100px"> <span style = "font-size: 9pt; color: # ff3300"> (for example, D: beifen) </span> </td>
</Tr>
<Tr>
<Td colspan = "3"> <asp: button ID = "Button1" runat = "server" Font-Size = "9pt" OnClick = "button#click" Text = "backup database"/> </td>
</Tr>
</Table>
Asp.net core code
Using System. Data. SqlClient;
Using System. IO;
String SqlStr1 = "Server = (local); DataBase = master; Uid = sa; Pwd = ";
String SqlStr2 = "Exec sp_helpdb ";
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 '";
Feature page
Using System. Data. SqlClient;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
String SqlStr1 = "Server = (local); DataBase = master; Uid = sa; Pwd = ";
String SqlStr2 = "Exec sp_helpdb ";
SqlConnection con = new SqlConnection (SqlStr1 );
Con. Open ();
SqlCommand com = new SqlCommand (SqlStr2, con );
SqlDataReader dr = com. ExecuteReader ();
This. DropDownList1.DataSource = dr;
This. DropDownList1.DataTextField = "name ";
This. DropDownList1.DataBind ();
Dr. Close ();
Con. Close ();
}
}
Protected void button#click (object sender, EventArgs e)
{
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 tutorial> 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
{
Con. Close ();
}
}
}
Restore database
String SqlStr1 = "Server = (local); database = '" + this. DropDownList1.SelectedValue + "'; Uid = sa; Pwd = ";
String SqlStr2 = "use master restore database" + dbname + "from disk = '" + path + "'";
Html code
<Table>
<Tr>
<Td style = "width: 100px; height: 21px"> <span style = "font-size: 9pt"> operational data warehouse </span> </td>
<Td> <asp: DropDownList ID = "DropDownList1" runat = "server" Font-Size = "9pt" Width = "124px"> </asp: DropDownList> </td>
<Td style = "width: 100px; height: 21px"> </td>
</Tr>
<Tr>
<Td style = "width: 100px"> <span style = "font-size: 9pt"> operational data warehouse </span> </td>
<Td style = "width: 100px"> <asp: fileUpload ID = "FileUpload1" runat = "server" Font-Size = "9pt" Width = "pixel PX"/> </td>
<Td style = "width: 100px">
</Td>
</Tr>
<Tr>
<Td colspan = "3"> <asp: button ID = "Button1" runat = "server" Font-Size = "9pt" OnClick = "button#click" Text = "Restore database"/> </td>
</Tr>
</Table>
Restore database page
Using System. Data. SqlClient;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
String SqlStr1 = "Server = (local); DataBase = master; Uid = sa; Pwd = ";
String SqlStr2 = "Exec sp_helpdb ";
SqlConnection con = new SqlConnection (SqlStr1 );
Con. Open ();
SqlCommand com = new SqlCommand (SqlStr2, con );
SqlDataReader dr = com. ExecuteReader ();
This. DropDownList1.DataSource = dr;
This. DropDownList1.DataTextField = "name ";
This. DropDownList1.DataBind ();
Dr. Close ();
Con. Close ();
}
}
Protected void button#click (object sender, EventArgs e)
{
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
{
Con. Close ();
}
}
}