简体   繁体   English

将静默SQL Server安装的输出重定向到C#中的标准错误?

[英]Redirect output from silent SQL Server installation to Standard Error in C#?

I have the following code: 我有以下代码:

Process p = new Process();
p.StartInfo.FileName = "SQLEXPRADV_x86_ENU.exe";
p.StartInfo.Arguments = "/CONFIGURATIONFILE=Config.ini";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;

p.Start();

string line;
while ((line = p.StandardOutput.ReadLine()) != null)
{
   tOutputStandard.Text += line + "\r\n";
}

while ((line = p.StandardError.ReadLine()) != null)
{
   tOutputError.Text += line + "\r\n";
}

p.WaitForExit();

The configuration file is set to run silently. 配置文件设置为静默运行。 tOutputStandard and tOutputError are TextBox's that will show the output. tOutputStandard和tOutputError是将显示输出的TextBox。

However it doesn't show any output from the SQL Server installation. 但是,它不显示SQL Server安装的任何输出。 I have tried running other applications, and they send the output correctly, so I don't think the code is at fault. 我已经尝试运行其他应用程序,并且它们正确地发送输出,所以我认为代码没有错。

How can I get SQL Server to pipe it's output to C#? 如何让SQL Server将其输出传递给C#? I need to know what the error is if installation fails. 如果安装失败,我需要知道错误是什么。

Maybe this helps. 也许这有帮助。 Delete your following code fragment: 删除以下代码片段:

string line;
while ((line = p.StandardOutput.ReadLine()) != null)
{
   tOutputStandard.Text += line + "\r\n";
}

while ((line = p.StandardError.ReadLine()) != null)
{
   tOutputError.Text += line + "\r\n";
}

Connect to the event handlers OutputDataReceived and ErrorDataReceived instead: 改为连接事件处理程序OutputDataReceived和ErrorDataReceived:

p.OutputDataReceived += this.OutputDataReceived;
p.ErrorDataReceived += this.ErrorDataReceived;

The callback methods look like this. 回调方法如下所示。

private void ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    tOutputError.Text += e.Data + "\r\n";
}

private void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    tOutputStandard.Text += e.Data + "\r\n";
}

So it turns out that the real cause to this problem, is that the SQL installation won't output anything if run directly from the MSI. 事实证明,导致此问题的真正原因是,如果直接从MSI运行,SQL安装将不会输出任何内容。

Instead, you have to first run the MSI, and extract it to a temp directory, and then run the setup exe in that directory. 相反,您必须首先运行MSI,并将其解压缩到临时目录,然后在该目录中运行setup exe。 This exe will give you the output you desire. 这个exe将为您提供所需的输出。

The answer as posted by Markus Safar with event handlers may also help for other needs. Markus Safar发布的带有事件处理程序的答案也可能有助于满足其他需求。

If you can't get the pipe working you can always just read the install log file. 如果无法使管道工作,您可以随时阅读安装日志文件。

I also use silent installs via Powershell. 我还通过Powershell使用静默安装。 For errors I look through the Setup Bootstrap log folders fro the latest folder and loop through the Details.txt file for lines containing "Error" 对于错误,我查看最新文件夹中的Setup Bootstrap日志文件夹,并循环显示Details.txt文件中包含“Error”的行

    C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap\Log\<datestamp>\Details.txt 

Not perfect, but better than no output at all. 不完美,但总比没有输出好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Express 2014静默安装不适用于C# - SQL Express 2014 silent installation not working with C# 在 C# 中重定向 CreateProcessWithTokenW 的标准输出 - Redirect Standard Output of CreateProcessWithTokenW in C# 如何在C#、. net中仅异步重定向标准错误流而不是进程的标准输出流 - How to asynchronously redirect ONLY standard error stream and not standard output stream of a process in C#, .net SQL Server和C#的输出不同 - Output from SQL Server and C# different C# 静默安装msi不起作用 - C# Silent installation of msi does not work C#运行没有新窗口的批处理脚本,并将所有(标准/错误)输出重定向到RichTextBox - C# run a batch script without a new window and redirect all (standard/error) output to a RichTextBox 如何在Windows窗体应用程序中的c#中将标准输出重定向到psexec进程中的文件 - How to redirect standard output to a file from psexec process in c# within a Windows Forms Application 将Python标准输入/输出重定向到C#表单应用程序 - Redirect Python standard input/output to C# forms application C#:重定向已经运行的进程的标准输出 - C#: Redirect Standard Output of a Process that is Already Running 从C#中的CreateProcessAsUser捕获标准输出 - Capture standard output from CreateProcessAsUser in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM