zoukankan      html  css  js  c++  java
  • c#mysql数据库备份还原

    1:引用dll

    MySql.Data.dll,   MySqlbackup.dll

    2:建一个数据连接静态类

    public static class mysql
    {
    public static string constr = "database=test;Password=密码;user ID=root;server=ip地址";
    public static MySqlConnection conn = new MySqlConnection(constr);
    }

    3:建winform窗体

    备份代码

    DialogResult result = MessageBox.Show("备份路径默认在当前程序下", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (result == DialogResult.Yes)
    {
    string time1 = System.DateTime.Now.ToString("d").Replace("/", "-");
    string file = ".//mysql/" + time1 + "_test.sql";
    using (MySqlCommand cmd = new MySqlCommand())
    {
    using (MySqlBackup mb = new MySqlBackup(cmd))
    {
    cmd.Connection = mysql.conn;
    mysql.conn.Open();
    mb.ExportToFile(file);
    mysql.conn.Close();
    MessageBox.Show("已备份");
    }
    }
    }
    else
    {
    return;
    }

    还原代码

    string file = textBox1.Text;
    if (file == "")
    {
    MessageBox.Show("不能为空");
    return;
    }
    DialogResult result = MessageBox.Show("确定还原吗?", "还原", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (result == DialogResult.Yes)
    {
    try
    {
    using (MySqlCommand cmd = new MySqlCommand())
    {
    using (MySqlBackup mb = new MySqlBackup(cmd))
    {
    cmd.Connection = mysql.conn;
    mysql. conn.Open();
    mb.ImportFromFile(file);
    mysql. conn.Close();
    MessageBox.Show("已还原");
    }
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    else
    {
    return;
    }

  • 相关阅读:
    【NOIP2016】换教室
    【NOIP模拟赛】总结
    【Codeforces Round 418】An impassioned circulation of affection DP
    DP测试总结
    【NOIP2012】疫情控制
    【HNOI2016】序列 莫队+单调栈+RMQ
    【Luogu P2709 小B的询问】莫队
    【HNOI2017】影魔
    【HNOI2017】大佬
    阿里云MaxCompute 2019-7月刊
  • 原文地址:https://www.cnblogs.com/ouyangkai/p/6526688.html
Copyright © 2011-2022 走看看