asp.net restore database and backup code

Source: Internet
Author: User
Tags trim

This is a very beautiful asp.net restore database tutorial and backup code Oh, okay. Let's take a look at the. NET tutorial to restore and back up the database code in the following example.

HTML code
<table>
<tr>
<TD style= "width:100px" ><span style= "font-size:9pt" > Operations Database </span></td>
<td><asp Tutorial: DropDownList id= "DropDownList1" runat= "Server" font-size= "9pt" width= "124px" ></asp:d Ropdownlist></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 "> (e.g. D:beifen) </span></td>
</tr>
<tr>
<TD colspan= "3" ><asp:button id= "Button1" runat= "Server" font-size= "9pt" onclick= "Button1_Click" text= "Backup Database "/></td>
</tr>
</table>

asp.net core code


Using System.Data.SqlClient;
Using System.IO;
String SqlStr1 = "server= (local);D atabase=master; Uid=sa; Pwd= ";
String SqlStr2 = "Exec sp_helpdb";
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 '";

Features 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);D atabase=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 Button1_Click (object sender, EventArgs e)
{
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 tutorial >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 ();
}
}
}

Restore Database


String SqlStr1 = "server= (local);d atabase= '" + 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 > Operations Database </span></td>
<td><asp:dropdownlist id= "DropDownList1" runat= "Server" font-size= "9pt" width= "124px" ></asp:d Ropdownlist></td>
<TD style= "width:100px; height:21px "></td>
</tr>
<tr>
<TD style= "width:100px" ><span style= "font-size:9pt" > Operations Database </span></td>
<TD style= "width:100px" ><asp:fileupload id= "FileUpload1" runat= "Server" font-size= "9pt" width= "190px"/> </td>
<TD style= "width:100px" >
</td>
</tr>
<tr>
<TD colspan= "3" ><asp:button id= "Button1" runat= "Server" font-size= "9pt" onclick= "button1_click" text= "RESTORE Database" "/></td>
</tr>
</table>

Restore Database Features 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);D atabase=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 Button1_Click (object sender, EventArgs e)
{
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 ();
}
}
}

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.