手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >ASP.NET下备份与还原数据库代码
ASP.NET下备份与还原数据库代码
摘要:核心技术:复制代码代码如下:usingSystem.Data.SqlClient;usingSystem.IO;stringSqlStr1=...

核心技术:

复制代码 代码如下:

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><span>操 作 数 据 库</span></td>

<td><asp:DropDownList ID="DropDownList1" runat="server" Font-Size="9pt" Width="124px"></asp:DropDownList></td>

<td></td>

</tr>

<tr>

<td><span>备份名称和位置</span></td>

<td><asp:TextBox ID="TextBox1" runat="server" Font-Size="9pt" Width="117px"></asp:TextBox></td>

<td><span>(如D:beifen)</span></td>

</tr>

<tr>

<td colspan="3"><asp:Button ID="Button1" runat="server" Font-Size="9pt" 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><span>操 作 数 据 库</span></td>

<td><asp:DropDownList ID="DropDownList1" runat="server" Font-Size="9pt" Width="124px"></asp:DropDownList></td>

<td></td>

</tr>

<tr>

<td><span>操 作 数 据 库</span></td>

<td><asp:FileUpload ID="FileUpload1" runat="server" Font-Size="9pt" Width="190px" /></td>

<td>

</td>

</tr>

<tr>

<td colspan="3"><asp:Button ID="Button1" runat="server" Font-Size="9pt" 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();

}

}

}

【ASP.NET下备份与还原数据库代码】相关文章:

ASP.NET数据库编程之Access连接失败

ASP.NET封装的SQL数据库访问类

ASP.NET与MySQL数据库简明图示入门教程

ASP.Net中数据展示控件的嵌套使用示例

ASP.NET编程精选25种函数源程序

ASP.NET中常用的三十三种代码第1/7页

ASP.NET程序中常用代码汇总

ASP.NET 高性能分页代码

ASP.NET连SQL7接口源代码

ASP.NET与数据库相关技巧

精品推荐
分类导航