时间:2016-02-24 15:15 来源:
我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【ASP.NET备份恢复SqlServer数据库】,下面是详细的分享!
ASP.NET备份恢复SqlServer数据库
备份SqlServer数据库
核心技术:
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(); } } }
|
以上所分享的是关于ASP.NET备份恢复SqlServer数据库,下面是编辑为你推荐的有价值的用户互动:
相关问题:sql server 2008怎么夸服务器备份数据库和恢复
答:给你个思路![192.168.0.135]机器上做个驱动映射到[192.168.0.4]机器共享的文件上;(驱动映射不会方法:共享[192.168.0.4... >>详细
相关问题:怎么备份sql2000 server 备份和恢复数据库所有的数据
答:新建个数据库,随便起个名 点到数据库,打开查询分析器 把SQL倒进去运行,或者用记事本打开.SQL格式的文件,复制进去,点击运行 如果没有语法错误,将提示影响....什么的 然后你再打开数据库,发现表里面就有SQL里的内容了 至于还原和备份,不用分析器语... >>详细
相关问题:SQL数据库如何自动备份和恢复?
答:、备份数据库 1、打开SQL企业管理器,在控制台根目录中依次点开Microsoft SQL Server 2、SQL Server组-->双击打开你的服务器-->双击打开数据库目录 3、选择你的数据库名称(如论坛数据库Forum)-->然后点上面菜单中的工具-->选择备份数据库 4、... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-