コマンドを実行し、ファイルから 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();
}