要从命令行转储数据库,我需要做的是:
mysqldump -uroot --password= myDb --routines> "C:\s.sql"
所以我想以编程方式尝试的就是这个,我想这是对其的直接解释:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = "-uroot --password= myDb --routines> \"C:\\s.sql\"";
Process process = Process.Start(psi);
process.WaitForExit();
process.Close();
这根本不起作用。相反,我必须去寻找这个,它可以在整个网络上找到,这也有效。
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format("-R -u{0} --password={1} -h{2} {3} --routines", "root", "", "localhost", "myDb");
Process process = Process.Start(psi);
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
using (StreamWriter writer = new StreamWriter("C:\\s.sql"))
{
writer.WriteLine(output);
writer.Close();
}
为什么我需要使用流编写器来获取 sql 文件中的数据库,而我可以直接从命令提示符中的命令执行此操作?
第二个 block 中 -R 的作用是什么?
最佳答案
您不能在参数中使用“">”重定向 stdout,因为这是命令提示符的一项功能。
-R 在转储中包含存储过程和函数。参见 http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines了解更多信息。
关于c# - 为什么以编程方式转储 MySQL 数据库与通过命令行转储不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9547960/