• 通过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();
            }

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

  • 相关阅读:
    常用的adb命令
    Jmeter之计数器
    Jmeter跨线程组传递变量
    Jmeter的属性和变量
    Jmeter之关联——常用提取器
    Jmeter常用的逻辑控制器
    HDU 1262 寻找素数对 模拟题
    HDU 1431 素数回文 离线打表
    HDU 2553 N皇后问题
    HDU 2093 考试排名 模拟题
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6751711.html
Copyright © 2020-2023  润新知