Mysql---C#在cmd中使用mysqldump导出sql文件

一、概述

本文描述了在C#中利用mysqldump工具导出sql文件。

二、代码片段

CmdHelper类代码如下:

    public class CmdHelper
    {
        public static 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");
            string output = p.StandardOutput.ReadToEnd();
            string myError = p.StandardError.ReadToEnd();
            p.WaitForExit();//等待程序执行完退出进程     
            p.Close();
            return myError;
        }
    }
string myDumpToolPath = @"C:\Program Files\MySQL\MySQL Server 5.5\bin";

string mySqlPath = myCurrentDirectory + "\\" + "SqlFile\\";
string myDumpCmd = $"mysqldump -uroot -p{myPwd} -B {myDbName} --add-drop-database>{mySqlPath}\\{mySqlFileName}";

myTask2 = new Task(() => { CmdHelper.RunCmd(myDumpToolPath, myDumpCmd); });

 

posted on 2018-11-16 09:45  缘惜  阅读(670)  评论(0编辑  收藏  举报