Article From:https://www.cnblogs.com/3xiaolonglong/p/9967547.html

I. overview

This paper describes the use of mysqldump tool to export SQL files in C #.

Code snippets

CmdHelperThe class code is as follows:

    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;//Accept input from the caller
            p.StartInfo.RedirectStandardOutput = true;//Getting output information by calling program
            p.StartInfo.RedirectStandardError = true;//Redirecting standard error output
            p.StartInfo.CreateNoWindow = true;//Do not display program window

            p.Start();
            p.StandardInput.WriteLine(strcmd );
            p.StandardInput.WriteLine("exit");
            string output = p.StandardOutput.ReadToEnd();
            string myError = p.StandardError.ReadToEnd();
            p.WaitForExit();//Waiting for the program to finish executing the exit process
            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); });

 

Leave a Reply

Your email address will not be published. Required fields are marked *