Reputation: 1353
I have written a C# program to execute bcp
command and transfer data from .txt
files into SQL Server tables. When I execute the command using command line it executes fine.
When I run the program shown below it gives this error:
at System.Diagnostics.Process.get_StandardError()
at ...StandardError has not been redirected.
Code:
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();
}
Upvotes: 0
Views: 3349
Reputation: 8849
add:
p.StartInfo.RedirectStandardError = true;
This is required if you want to access p.StandardError
later in the code.
Upvotes: 1