2010-12-15 140 views
3

我正在嘗試使用C#代碼從轉儲文件恢復MySQL數據庫數據。C#MySQL數據庫恢復

我想執行以下命令: MySQL的--verbose --user =根--password = qwerty123456測試< C:\用戶\默認\ testing.SQL

我知道C#沒有按」不承認「<」符號,所以我嘗試了幾種方法,但仍然無效。任何人都可以幫助我嗎?我想使用C#代碼將所有數據庫數據恢復到MySQL中。

在此先感謝。

  Process process = new Process(); 
      process.StartInfo.FileName = @"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"; 
      process.StartInfo.Arguments = @"--verbose --user=root --password=qwerty123456 test"; 
      process.StartInfo.UseShellExecute = false; 
      process.StartInfo.RedirectStandardOutput = true; 
      process.StartInfo.RedirectStandardInput = true; 
      process.StartInfo.RedirectStandardError = true; 
      process.StartInfo.CreateNoWindow = true; 
      process.Start(); 

      StreamReader sr = process.StandardOutput; 
      sr = File.OpenText(@"C:\Users\Default\testing.SQL"); 

回答

2

<處理應(IIRC)是確定的,如果你簡單地設置UseShellExecute = true

但是,如果你真的想避免外殼EXEC,<輸入 - 你應該將文件寫入StandardInput。我可能單獨留下StandardOutput(如果您不主動需要輸出,請設置RedirectStandardOutput = false)。

未經檢驗的,但也許:

 using(var stdin = process.StandardInput) 
     using(var reader = File.OpenText(@"C:\Users\Default\testing.SQL")) { 
      string line; 
      while((line = reader.ReadLine()) != null) { 
       stdin.WriteLine(line); 
      } 
      stdin.Close(); 
     } 

(應該由行的文件行管)

+0

謝謝!有用。 – athgap 2010-12-15 08:16:39