2011-02-28 114 views
2

我有要求使用像這樣的批處理文件來運行一組* .sql文件。如何將參數傳遞給批處理文件?

private void btn_Execute_Click(object sender, EventArgs e) 
{ 
    try { 
    //Creating A batch file to execute the scripts using SQLPLUS.... 
    FileInfo fi5 = new FileInfo("c:/EMPSCRIPTS/execute.bat"); 
    StreamWriter sw2 = fi5.CreateText(); 
    sw2.WriteLine("@Echo Off \r \nsqlplus scotte/[email protected] @\"c:/EMPSCRIPTS/RUNALL.sql\" \r \nEXIT "); 
    sw2.Close(); 

    System.Diagnostics.Process proc; // Declare New Process 
    proc = System.Diagnostics.Process.Start(ConfigurationManager.AppSettings["BatFilePath"].ToString()); // run test.bat from command line. 
    proc.StartInfo.UseShellExecute = false; 
    proc.StartInfo.RedirectStandardOutput = true; 
    proc.StartInfo.FileName = "cmd.exe"; 
    proc.StartInfo.Arguments = "/c" + ConfigurationManager.AppSettings["BatFilePath"].ToString(); 
    proc.StartInfo.RedirectStandardError = true; 
    proc.StartInfo.RedirectStandardInput = true; 
    proc.StandardOutput.ReadToEnd(); 
    proc.WaitForExit(); 
    proc.Close(); 
    } 
    catch (Exception ex) { 
    MessageBox.Show("Insertion Completed"); 
    } 

} 

但我想停止一些不必要的文件執行。我找到了傳遞參數的選項。我想靜態給參數文件。根據參數必須執行的用戶輸入。任何人都可以幫助我嗎? 在此先感謝。

+0

此外''\ r \ n「'是錯誤的。那裏不應該有空間。 – SLaks 2011-02-28 14:29:28

回答

1

您錯用了Process.Start

您需要創建一個新的ProcessStartInfo對象,設置其所有屬性,然後將其傳遞給Process.Start

修改正在運行的進程StartInfo,因爲您的代碼正在執行,不起作用。

2

在進程啓動後,您正在更改StartInfo的值,這對進程沒有影響。 See the "Remarks" section here for more information.

創建一個ProcessStartInfo的新實例,將其設置爲您需要的值,然後將其傳入the overload of Start,該實例需要此類型的實例。

另外,一旦你改變你的代碼,你可以跳過將命令行寫入批處理文件。您的executable filenamesqlplus,您的argumentsscotte/[email protected] @\"c:/EMPSCRIPTS/RUNALL.sql\"

相關問題