2010-05-17 17 views
3

嗨根據我的最後一個問題here我嘗試編寫一個sql編輯器或類似的東西,這樣我嘗試從C#連接到CMD並執行我的命令。 現在我的問題是我連接到SQLPLUS後,我不能得到SQLPLUS命令,我審查的其他資源不滿足我。請問我連接到sqlplus後如何實現我的進程來運行我的sql命令?現在我用這個代碼:使用C#執行多個命令行使用相同的進程

//Create process 
System.Diagnostics.Process pProcess = new System.Diagnostics.Process(); 

//strCommand is path and file name of command to run 
pProcess.StartInfo.FileName = strCommand; 

//strCommandParameters are parameters to pass to program 
pProcess.StartInfo.Arguments = strCommandParameters; 

pProcess.StartInfo.UseShellExecute = false; 

//Set output of program to be written to process output stream 
pProcess.StartInfo.RedirectStandardOutput = true; 

//Optional 
pProcess.StartInfo.WorkingDirectory = strWorkingDirectory; 

//Start the process 
pProcess.Start(); 

//Get program output 
string strOutput = pProcess.StandardOutput.ReadToEnd(); 

//Wait for process to finish 
pProcess.WaitForExit(); 

我定製了它。我單獨初始化,我創建的進程對象有一次我仍然有問題,跑我使用這些代碼對第二個呼叫第二個命令:

pProcess.StartInfo.FileName = strCommand; 

//strCommandParameters are parameters to pass to program 
pProcess.StartInfo.Arguments = strCommandParameters; 
//Start the process 
pProcess.Start(); 

//Get program output 
string strOutput = pProcess.StandardOutput.ReadToEnd(); 

//Wait for process to finish 
pProcess.WaitForExit(); 

在此先感謝

+4

FWIW,這些評論只是噪音... – 2010-05-17 18:07:34

+0

你的意思是你想開始一次的過程,但發送多個命令嗎?如果是這樣,爲什麼不發送多個命令從頭開始整個過程​​從頭開始每次(雙關語不打算,但無論如何有趣) – 2010-05-17 19:17:44

+0

正好... 我只是運行一次sqlplus 後,我給用戶/通行證,如果它登錄發送Sql.PL命令。 我馬有點困惑:( actully每次我嘗試連接數據庫並運行我的命令需要時間...但我沒有任何想法,也許其他軟件使用像這樣?哈?任何想法? – Amir 2010-05-18 19:37:36

回答

6

你的問題有點令人困惑,但我認爲我看到你的問題。首先,你應該看看這篇博文,看看common issues with System.Diagnostics.Process。你的代碼碰巧違反了那裏沒有列出的代碼。 Process對象本身的重用。

您需要重構像代碼:

class MyProcessStarter 
    { 
     private ProcessStartInfo _startInfo = new ProcessStartInfo(); 
     public MyProcessStarter(string exe, string workingDir) 
     { 
      _startInfo.WorkingDirectory = workingDir; 
      _startInfo.FileName = exe; 
      _startInfo.UseShellExecute = false; 
      _startInfo.RedirectStandardOutput = true; 
     } 

     public string Run(string arguments) 
     { 
      _startInfo.Arguments = arguments; 
      Process p = Process.Start(_startInfo); 
      p.Start(); 
      string strOutput = p.StandardOutput.ReadToEnd(); 
      p.WaitForExit(); 
      return strOutput; 
     } 
    } 

我寫了一個更完整,更準確地執行所謂的ProcessRunner。下面演示它用於執行相同的操作:

using CSharpTest.Net.Processes; 
partial class Program 
{ 
    static int Main(string[] args) 
    { 

     ProcessRunner run = new ProcessRunner("svn.exe"); 
     run.OutputReceived += new ProcessOutputEventHandler(run_OutputReceived); 
     return run.Run("update", "C:\\MyProject"); 
    } 

    static void run_OutputReceived(object sender, ProcessOutputEventArgs args) 
    { 
     Console.WriteLine("{0}: {1}", args.Error ? "Error" : "Output", args.Data); 
    } 
} 
+0

謝謝老兄,第一個代碼很有用,但我仍然有錯誤!!! 您知道嗎,我的進程是這樣​​運行的: 我將它翻譯成exec命令: sqlplus user/pass @ db sqlplus prompt「amir」; <== this我只是sqlplus運行一次,之後我只是發送參數並執行它... 和第二個實用程序我得到錯誤!我收到空異常指針,我也嘗試使用它自己的testRunFile,但在那一個也是一樣的。我無法找到「斷言」類的任何地方??:(( 幫我plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzz。 – Amir 2010-05-18 19:43:00

1

在發送另一個命令之前,您需要從輸入中讀取所有數據!

如果沒有數據可用,你不能要求閱讀...一點點吸不是可行的嗎?

我的解決方案......當問到讀......請閱讀大緩衝......像1個MEGA ...

而且你會需要等待一分鐘100毫秒......示例代碼。 ..

Public Class Form1 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim oProcess As New Process() 
     Dim oStartInfo As New ProcessStartInfo("cmd.exe", "") 
     oStartInfo.UseShellExecute = False 
     oStartInfo.RedirectStandardOutput = True 
     oStartInfo.RedirectStandardInput = True 
     oStartInfo.CreateNoWindow = True 
     oProcess.StartInfo = oStartInfo 
     oProcess.Start() 

     oProcess.StandardInput.WriteLine("dir") 


     Threading.Thread.Sleep(100) 

     Dim Response As String = String.Empty 

     Dim BuffSize As Integer = 1024 * 1024 
     Dim bytesRead As Integer = 0 

     Do 

      Dim x As Char() = New Char(BuffSize - 1) {} 
      bytesRead = oProcess.StandardOutput.Read(x, 0, BuffSize) 

      Response = String.Concat(Response, String.Join("", x).Substring(0, bytesRead))    

     Loop While oProcess.StandardOutput.Peek >= 0 



     MsgBox(Response) 
     Response = String.Empty 



     oProcess.StandardInput.WriteLine("dir c:\") 




     Threading.Thread.Sleep(100) 


     bytesRead = 0 

     Do 

      Dim x As Char() = New Char(BuffSize - 1) {} 
      bytesRead = oProcess.StandardOutput.Read(x, 0, BuffSize) 

      Response = String.Concat(Response, String.Join("", x).Substring(0, bytesRead)) 
      'Response = String.Concat(Response, String.Join("", x)) 

     Loop While oProcess.StandardOutput.Peek >= 0 

     MsgBox(Response) 


    End Sub 
End Class