| ①创建Web应用程序窗体
在Web窗体(DataBase2.aspx)上放置一个按钮: <asp:Button ID="BackUpBtn" runat="server" Text=" 备份数据库 " OnClick="BackUpBtn_Click" />
再添加一个Label控件: <asp:Label ID="MsgLabel" runat="server"></asp:Label>
②添加引用的空间名称
using System; using System.IO;
③备份SQL数据库的代码
//备份数据库 protected void BackUpBtn_Click(object sender, EventArgs e) { string DbPath1, DbPath2, DbName4DbPath2;
DbName4DbPath2 = DateTime.Now.ToString().Replace(":","."); DbPath1 = Server.MapPath("../App_Data/DataBase.mdb"); DbPath2 = Server.MapPath("../App_Data/" + DbName4DbPath2 + ".mdb");
try { File.Copy(DbPath1, DbPath2, true);
MsgLabel.Text = "数据库备份成功到" + DbName4DbPath2 + ".mdb!"; } catch { MsgLabel.Text = "数据库备份失败,请重试!"; MsgLabel.CssClass = "redColor"; } } |