ASP.NET备份恢复SqlServer数据库
2019-06-20 08:48:32来源: 阅读 ()
核心技术:
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();
}
}
};
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- asp.net源程序编译为dll文件并调用的实现过程 2020-03-29
- Asp.net MVC SignalR来做实时Web聊天实例代码 2020-03-29
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据 2020-03-29
- Asp.Net中WebForm的生命周期 2020-03-29
- ASP.NET使用Ajax返回Json对象的方法 2020-03-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash