0

コマンドを実行し、ファイルから SQL Server テーブルにbcpデータを転送する C# プログラムを作成しました。.txtコマンドラインを使用してコマンドを実行すると、正常に実行されます。

以下に示すプログラムを実行すると、次のエラーが発生します。

System.Diagnostics.Process.get_StandardError()
で ...StandardError がリダイレクトされていません。

コード:

string outputfilename = @"C:\Output.txt";
string cmdExe = "MyDB.dbo.a in outputfilename -c -T -S servername\\DEV -U readonly -P readonly -F2";
 System.Diagnostics.Process p = new System.Diagnostics.Process();
 p.StartInfo.WorkingDirectory = @"C:\\";
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.CreateNoWindow = true;
 p.StartInfo.FileName = "BCP";


 p.StartInfo.Arguments = cmdExe;

 try
 {
     p.Start();
     p.BeginOutputReadLine();

     StreamReader myStreamReader = p.StandardError;
     // Read the standard error of net.exe and write it on to console.
    Console.WriteLine(myStreamReader.ReadLine());
 }
 catch (Exception e)
 {
     Console.WriteLine(e.StackTrace.ToString());
     Console.WriteLine(e.Message);
     Console.ReadLine();
 }
 if (p.WaitForExit(100))
 {
     // Process completed. Check process.ExitCode here.
     p.StandardOutput.ReadToEnd();
 }
4

1 に答える 1

1

追加:

p.StartInfo.RedirectStandardError = true;

p.StandardErrorこれは、後でコードにアクセスする場合に必要です。

于 2013-10-17T19:38:24.997 に答える