Dzwebs.Net

撰写电脑技术杂文十余年

asp.net(C#)备份SQL数据库的代码

Admin | 2009-11-2 12:31:12 | 被阅次数 | 7666

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  ①创建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";
   }
  }


该杂文来自: 网站开发杂文

上一篇:asp.net(C#)压缩数据库的代码

下一篇:数据库连接超时、连接达到最大池大小的主观原因

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计