C# and mysqldump

2020-05-02 03:51发布

I'm writing an application which should make a complete copy of one database and then import it on the same server with different name.
So, I guess I should use mysqldump and mysql and the parameters that I should pass to them.
Okay, but I can't get the dump put the file where I want, because I have to know the location and then pass it to mysql.

StringBuilder exportPath = new StringBuilder();
//exportPath.Append(Directory.GetCurrentDirectory());
exportPath.Append(@" > C:\Temp\backup.sql");

Process MySQLDump = new Process();
MySQLDump.StartInfo.UseShellExecute = true;
//MySQLDump.StartInfo.RedirectStandardOutput = true;
MySQLDump.StartInfo.FileName = "mysqldump";
MySQLDump.StartInfo.Arguments = "-u root -proot -h localhost mytable" + exportPath;
MySQLDump.Start();
//string theDump = MySQLDump.StandardOutput.ReadToEnd();
MySQLDump.WaitForExit();
MySQLDump.Close();

I'm doing something wrong, but I don't know what.

1条回答
迷人小祖宗
2楼-- · 2020-05-02 04:16

You have two choices:

  • Do not redirect the output of mysqldump in the command line, but use a more verbouse process creation and hook the standard output of mysqldump. This makes ist possible to postprocess the file (Hash it as an example) and either write it out to where you want, or run it directly into the standard input of the importing instance.
  • Understand, that > C:\\Documents and Settings\\admin\\Desktop\\databases\\db.sql is not a command line parameter. You need to sssemble and the parameter and redirection part string and use shell execution. Make sure, you use an absolute path name.
查看更多
登录 后发表回答