zoukankan      html  css  js  c++  java
  • 通过c# 实现mysql 数据库的备份和附加

    近期涉及到通过c# 对mysq数据库的备份和附件功能

    由于mysql 有类似的备份和附加的cmd命令。可是一直没用过,今天实践了下,感觉效率挺快。比自己写的效率高。以下我列出c#调用mysql的备份和附加功能函数。

    1.备份mysql数据库

     定义string strAddress = string.Format("mysqldump --host={0} --default-character-set=utf8 --lock-tables  --routines --force --port=3306 --user={1} --password={2} --quick  ", 连接的server名称, username, 密码);

    string strDB=你须要备份的数据库名称。

    this.mysqlPath = "C:\Program Files\MySQL\MySQL Server 5.5\bin";

    if (!string.IsNullOrEmpty(strDB))
                    {
                        sfd.Filter = "数据库文件|*.sql";
                        sfd.FilterIndex = 0;
                        sfd.RestoreDirectory = true;
                        sfd.FileName = "BackUp-" + strDB + DateTime.Now.ToString("yyyyMMDDHHmmss") + ".sql";
                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            string filePath = sfd.FileName;
                            string cmd = this.strAddress + strDB + " >" + filePath;
                            string result = RunCmd(m_mysqlPath, cmd);
                            if (result.Trim() == "")
                            {
                                Show("数据库备份成功!", "提示", );
                            }
                            else
                            {
                               Show(result, "提示");
                            }
                        }
                    }

    主要执行函数

    private string RunCmd(string strPath, string strcmd)
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.WorkingDirectory = strPath;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;

                p.Start();
                p.StandardInput.WriteLine(strcmd);
                p.StandardInput.WriteLine("exit");        
                return p.StandardError.ReadToEnd();
            }

    运行就可以对选中数据库进行备份。

  • 相关阅读:
    传输线
    互连设计
    数字IC·功耗
    [z]一个合格的FPGA工程师需要掌握哪些知识?
    Arduino I2C + DS1307实时时钟
    Arduino I2C + 温湿度传感器Si7021
    Arduino I2C + 温湿度传感器AM2321
    Arduino I2C + 数字式环境光传感器BH1750FVI
    什么是“光照度(Illuminance)”?
    Arduino ADC + 模拟温度传感器LM35D
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6751711.html
Copyright © 2011-2022 走看看