原文发布时间为:2008-10-25 —— 来源于本人的百度文章 [由搬家工具导入]

数据库备份与还原c#.net实现:

页面上面有 备份,还原,下拉菜单(浏览备份文件夹下面的所有文件名),删除(删除备份),这四个控件。。。。。备份的时候把 时间转化成字符串格式 然后作为文件名保存 防止重复。

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="备份" /><br />
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList><br />
        <br />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="还原" />
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="删除" /></div>
    </form>
</body>
</html>

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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)
        {
            BindFileName();
        }

}
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "Backup Database pubs to DISK='" + Server.MapPath("App_Data//BackUp//pubs" + DateTime.Now.ToString("yyyyMMddhhmmss")) + "'";
        SqlConnection pubsConn = new SqlConnection("Server=.\\SQLEXPRESS;Database=pubs;Integrated Security=True");
        SqlCommand cmd = new SqlCommand(sql, pubsConn);
        pubsConn.Open();
        int a=cmd.ExecuteNonQuery();
        if (a == -1)
        {
            Response.Write("success!");
        }
        else
        {
            Response.Write("fail!");
        }
        pubsConn.Close();
        BindFileName();
    }

public void BindFileName()
    {
        DropDownList1.Items.Clear();
            string path = Server.MapPath("App_Data//BackUp");
            DirectoryInfo di = new DirectoryInfo(path);
            foreach (FileInfo fi in di.GetFiles())
            {           
                ListItem li=new ListItem();
                li.Value=fi.FullName;
                li.Text=fi.Name;
                DropDownList1.Items.Add(li);
            }
    }
protected void Button2_Click(object sender, EventArgs e)
{
string sql = "Restore Database pubs From DISK='" + DropDownList1.SelectedValue + "'";
        SqlConnection masConn = new SqlConnection("Server=.\\SQLEXPRESS;Database=master;Integrated Security=True");
        SqlCommand cmd = new SqlCommand(sql, masConn);
        masConn.Open();
        int a=cmd.ExecuteNonQuery();
        if (a == -1)
        {
            Response.Write("success!");
        }
        else
        {
            Response.Write("fail!");
        }
        masConn.Close();

}
    protected void Button3_Click(object sender, EventArgs e)
    {
        File.Delete(DropDownList1.SelectedValue);
        BindFileName();
    }
}

转载于:https://www.cnblogs.com/handboy/p/7148456.html

数据库备份与还原c#.net实现相关推荐

  1. Asp.net 不使用SQLDMO实现数据库备份和还原

    今天需要做一个历史数据库,备份还原的程序,就是在sql服务器兴建一个数据库的历史版本的复.手工操作很方便,但是程序来实现我还没做过.上网找资料发现都调用了 SQLDMO 的 SQL COM 来实现. ...

  2. MySQL数据库备份和还原的常用命令小结

    MySQL数据库备份和还原的常用命令小结,学习mysql的朋友可以参考下. 备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword datab ...

  3. MySQL数据库备份和还原的常用命令

    MySQL数据库备份和还原的常用命令 2012-03-27 14:43:34 标签:linux mysql 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追 ...

  4. postgresql数据库备份与还原

    postgresql数据库备份与还原 备份数据库: $ su - postgres $ pg_dump testdb > testdb.sql 备份单个表: $ pg_dump --table= ...

  5. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第一部分

    sql还原数据库备份数据库 So far, we've discussed a lot about database backup-and-restore process. The backup da ...

  6. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第二部分

    sql还原数据库备份数据库 In this article, we'll walk through, some of the refined list of SQL Server backup-and ...

  7. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第IV部分

    sql还原数据库备份数据库 In this article, we'll see the how the backup-and-restore meta-data tables store the i ...

  8. sql还原数据库备份数据库_有关数据库备份,还原和恢复SQL面试问题–第三部分

    sql还原数据库备份数据库 So far, we've discussed a lot about database backup commands. In this article, we'll d ...

  9. SQL Server数据库备份和还原报告

    In the previous articles, we discussed several ways of taking backup and testing the restore process ...

  10. linux怎么把mysql数据库备份还原,MySQL数据库备份和还原

    MySQL数据库备份和还原 打开cmd命令行,一定不是mysql的命令行,我第一次就错在这个地方,郁闷了很久 备份MySQL数据库的命令 mysqldump -hhostname -uusername ...

最新文章

  1. python有哪些关键字?让他自己“吐”出来!
  2. 李伯虎院士:新一代人工智能引领下的智造制造初步实践
  3. CENTOS/RHEL 7 系统中设置SYSTEMD SERVICE的ULIMIT资源限制
  4. 编程学习记录12:Oracle数据库的一些基本操作2,表相关操作,添加约束
  5. Vue CLI 3.0脚手架如何在本地配置mock数据json
  6. 亚信安全协助绿谷制药确保“秘方”安全
  7. c6011取消对null指针的引用_COM编程攻略(二十二 IDL中的枚举,指针,数组)
  8. 贴吧粉丝怎么全部移除_亚马逊FBA怎么发货?怎么把货发到FBA仓库?
  9. java 正则 工具类_正则表达式工具类,正则表达式封装,Java正则表达式
  10. (整理)RMAN备份详解
  11. centos7修改ip地址命令_linux nmcli命令详解
  12. select count(*) ,count() , select *
  13. jeesite图片上传并显示
  14. TeamViewer正版许可证到底多少钱?
  15. 计算机微信接收excel打不开怎么回事,电脑端微信打不开怎么解决
  16. BottomNavigationView修改图标/文字大小,替换图标
  17. Dazdata BI之PDF魔幻输出
  18. 小熊、九阳、苏泊尔,小家电玩家们乱了阵脚?
  19. 马蜂窝 iOS App 启动治理:回归用户体验
  20. XGen工具箱提示 gxgmTMLookupTable是未声明的变量

热门文章

  1. centos 6.5环境利用iscsi搭建SAN网络存储服务及服务端target和客户端initiator配置详解...
  2. Ext.Net系列:二Event之DirectEvent 示例2(Delay)
  3. JS 在 HTML 中做加减乘除
  4. .NET Mail : 注意Win 7 不再包含SMTP服务
  5. 使用ISA Server 2006发布Exchange Server 2007安全的Web、安全的OWA和Outlook Anyw
  6. 三、定义主从实体基类
  7. 在Redis集群技术上,你不可错过的四大集成者
  8. 编程科普书籍推荐(Java)
  9. Spring 注解@Resource @Autowired @Service @Component
  10. 字节实习生开发的 AI 竟然被网友用在了王冰冰身上!