C# ASP.NET使用开源作品(MysqlBackup.Net) 备份和还原MySQL数据库

使用了以下网页链接  https://www.cnblogs.com/dsnixi/p/4951241.html 提供的下载链接

           点击下载MysqlBackup.Net

下载解压后得到的MysqlBackup.Net最大只支持.NET4.5,参见下图

 

通过添加引用添加到我的工程项目(我的工程项目为.NET4.6.1),运行后报错,参见下图。

原因是我电脑正在使用的MySQL数据库版本和下载链接下载的MysqlBackup.Net对应的MySQL数据库版本不一致导致的。

 

解决办法

 

一、使用NuGet下载支持.NET4.6.1以及支持我电脑正在使用的MySQL版本的MysqlBackup

二、搜索和下载MysqlBackup

 

下图中,红色1可以选择最新版本,红色2可以开始安装包。

 

点击【确定】按钮

点击【我接受】按钮

正在安装

安装完毕

三、添加备份数据库代码

注意:最好不要备份到C盘,因为操作系统有权限限制。

      protected void Button9_Click(object sender, EventArgs e)
        {
            string constring = "server=localhost;port=3306;user Id=root;password=68331; database=userreco ;Allow User Variables=True"; //连接字符串
            string file = "D:\\backup.sql";
            using (MySqlConnection conn = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ExportToFile(file);
                        conn.Close();
                    }
                }
            }
        }

四、添加恢复数据库代码

     protected void Button10_Click(object sender, EventArgs e)
        {
            string constring = "server=localhost;port=3306;user Id=root;password=68331; database=userreco ;Allow User Variables=True"; //连接字符串
            string file = "D:\\backup.sql";
            using (MySqlConnection conn = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ImportFromFile(file);
                        conn.Close();
                    }
                }
            }
        }

五、测试

第一步:执行备份。

第二步:用MySQL Workbench软件将userreco数据库table1中的记录全部删除。

第三步:关闭MySQL Workbench软件。

第四步:再进入MySQL Workbench软件确认userreco数据库table1中的记录数为零。

第五步:执行恢复

第六步:再进入MySQL Workbench软件确认userreco数据库table1中的记录数恢复到备份时刻的数据。

 

六、完整的源程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MySql.Data.MySqlClient;


namespace WebApplication1
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

     protected void Button9_Click(object sender, EventArgs e)
        {
            string constring = "server=localhost;port=3306;user Id=root;password=68331; database=userreco ;Allow User Variables=True"; //连接字符串
            string file = "D:\\backup.sql";
            using (MySqlConnection conn = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ExportToFile(file);
                        conn.Close();
                    }
                }
            }
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            string constring = "server=localhost;port=3306;user Id=root;password=68331; database=userreco ;Allow User Variables=True"; //连接字符串
            string file = "D:\\backup.sql";
            using (MySqlConnection conn = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ImportFromFile(file);
                        conn.Close();
                    }
                }
            }
        }
   }
}

至于如何添加MySQL的引用请参考我的博客或其它博客。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值