欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET下备份与还原数据库代码

程序员文章站 2024-03-08 20:01:16
核心技术: 复制代码 代码如下: using system.data.sqlclient; using system.io; string sqlstr1 = "serve...
核心技术:
复制代码 代码如下:

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