簡體   English   中英

為什么轉儲MySQL數據庫在程序上不同於通過命令行轉儲?

[英]Why is dumping MySQL database programmatically different from dumping via command line?

要從命令行轉儲數據庫,我需要做的是:

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在第二個塊中起什么作用?

  1. 您不能在參數中使用“>”重定向標准輸出,因為這是命令提示符的功能。

  2. -R在轉儲中包括存儲過程和函數。 有關更多信息,請參見http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines

在命令行版本中,您正在執行的操作是使用Shell將標准輸出通過管道傳遞到文件( >命令,后跟文件名,是一種簡單的方式,說“獲取該程序的所有標准輸出並編寫到此文件”)。 要在C#中執行相同的操作,您需要自己處理標准輸出並將其寫入文件。

第二個示例中的-R似乎是重復的。 根據此頁面 ,它與--routines相同。 你有沒有試過嗎?

我以為我會以編程方式包括“參數”的外觀,在本例中,我們還希望將數據庫的事件轉儲到文件中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM