程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET下備份與還原數據庫代碼

ASP.NET下備份與還原數據庫代碼

編輯:ASP.NET基礎
核心技術:
復制代碼 代碼如下:
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'";

1.前台
復制代碼 代碼如下:
<table>
<tr>
<td style="width: 100px"><span style="font-size: 9pt">操 作 數 據 庫</span></td>
<td><asp: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">備份名稱和位置</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">(如D:\beifen)</span></td>
</tr>
<tr>
<td colspan="3"><asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="備份數據庫" /></td>
</tr>
</table>

2.後台
復制代碼 代碼如下:
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 Button1_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>alert('此文件已存在,請從新輸入!');location='Default.aspx'</script>");
return;
}
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('備份數據成功!');location='Default.aspx'</script>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('備份數據失敗!')</script>");
}
finally
{
con.Close();
}
}
}



還原SqlServer
核心技術:
復制代碼 代碼如下:
string SqlStr1 = "Server=(local);database='" + this.DropDownList1.SelectedValue + "';Uid=sa;Pwd=";
string SqlStr2 = "use master restore database " + dbname + " from disk='" + path + "'";

1.前台
復制代碼 代碼如下:
<table>
<tr>
<td style="width: 100px; height: 21px"><span style="font-size: 9pt">操 作 數 據 庫</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">操 作 數 據 庫</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="還原數據庫" /></td>
</tr>
</table>

2.後台
復制代碼 代碼如下:
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 Button1_Click(object sender, EventArgs e)
{
string path = this.FileUpload1.PostedFile.FileName; //獲得備份路徑及數據庫名稱
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('還原數據成功!');location='Default.aspx'</script>");
}
catch (Exception error)
{
Response.Write(error.Message);
Response.Write("<script language=javascript>alert('還原數據失敗!')</script>");
}
finally
{
con.Close();
}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved