asp.net 在线备份SQL数据库

BackUpDB.aspx

View Code
1 <div>
2
3 备份数据库:<asp:DropDownList ID="db" runat="server">
4 </asp:DropDownList>
5 <br />
6 <br />
7
8 </div>
9 <div>
10
11 备份名称和位置:<asp:TextBox ID="positions" runat="server"></asp:TextBox>
12 <br />
13 <asp:Button ID="btBK" runat="server" onclick="btBK_Click" Text="开始备份" />
14 <br />
15
16 </div>

BackUpDB.aspx.cs

View Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!Page.IsPostBack)
4 {
5 BindDB();
6 }
7 }
8
9 private void BindDB()
10 {
11 DataSet ds = SQLHelper.Query ("Exec sp_helpdb");
12 db.DataSource = ds;
13 db.DataTextField = "name";
14 db.DataBind();
15 }
16
17 protected void btBK_Click(object sender, EventArgs e)
18 {
19 string SQL_STR = "backup database " + db.SelectedValue + " to disk='" + positions.Text.Trim() + ".bak'";
20 try
21 {
22 if (File.Exists(positions.Text.Trim()))
23 {
24 Response.Write("<script>alert('此文件己存在,请重新输入!');location='BackUpDB.aspx'</script>");
25 return;
26 }
27 SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionStr, CommandType.Text, SQL_STR, null);
28 Response.Write("<script>alert('备份数据成功!');location='BackUpDB.aspx'</script>");
29 }
30 catch (Exception ee)
31 {
32 Response.Write(ee.Message);
33 Response.Write("<script>alert('备份数据库失败!')</script>");
34 }
35
36 }
posted @ 2011-07-06 14:17  Jack Qin  阅读(308)  评论(0编辑  收藏  举报
作者:Jack Qin http://www.nosqlcn.com