为什么以编程方式转储MySQL数据库与通过命令行转储不同?

时间:2012-03-03 17:02:22

标签: c# .net mysql mysqldump startprocessinfo

要从命令行转储数据库,我需要做的就是:

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();
}
  1. 为什么我需要使用流编写器来获取sql文件中的数据库,否则我可以直接从命令提示符中的命令执行此操作?

  2. -R在第二个区块中的作用是什么?

3 个答案:

答案 0 :(得分:2)

  1. 您无法使用“>”重定向标准输出在参数中,因为这是命令提示符的一个功能。

  2. -R包括转储中的存储过程和函数。有关详细信息,请参阅http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines

答案 1 :(得分:1)

您在命令行版本中所做的是使用shell将标准输出传递给文件(>命令,后跟文件名,是一种简单的说法“该程序的标准输出并将其写入此文件“)。要从C#执行相同的操作,您需要自己处理标准输出并将其写入文件。

第二个示例中的-R似乎是重复的。根据{{​​3}},它与--routines相同。你没试过吗?

答案 2 :(得分:0)

我认为我会以编程方式包含Arguments的内容,在我们的例子中,我们还希望将DB的事件转储到文件中。

psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "someUser", "xxxxxx", "localhost", dbName, "--routines","--events");
相关问题