从我的流程中获取输出

时间:2012-05-07 10:26:16

标签: c#

我正在使用sqlcmd(来自我的应用程序)查询sql server的版本,并希望在富文本框中显示信息,我该怎么做,这是代码:

        Process proc = new Process();
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.FileName = "sqlcmd";
        proc.StartInfo.Arguments = @"-U sa -P somepassword -q 'SELECT @@VERSION' -S (localhost)\InstanceName";
        proc.Start();    


        StringBuilder q = new StringBuilder();
        while (!proc.HasExited)
        {
            q.Append(proc.StandardOutput.ReadToEnd());

        }
        string r = q.ToString();
        rtbCheckVersion.Text = r;
        proc.WaitForExit();

2 个答案:

答案 0 :(得分:2)

因为您必须执行sql脚本,所以可以使用SqlConnection和SqlCommand来获取输出,而不是启动一个单独的进程。

在此问题中查看此答案和其他答案以获取一些示例:https://stackoverflow.com/a/949933/559144

答案 1 :(得分:0)

我发现您不想使用SqlConnectionSqlCommand ...
我不确定,但可能是你的流程在输出之前退出 试试这个:

proc.Start();    
string s = proc.StandardOutput.ReadToEnd();
相关问题