question

PadmanabhanVenkatesh-6789 avatar image
0 Votes"
PadmanabhanVenkatesh-6789 asked ErlandSommarskog commented

BCP not returning any rows

Hi. I am trying to use the BCP tool in a process ( .NET console application)

There are times, when the data is getting passed, there are times the BCP is not passing any data. How to find if there is any error while the BCP is executing. I have tried the below code, but not been able to capture any error.

using (var process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "BCP";

             process.StartInfo.Arguments = "\"" + Query + " \" queryout " + "\"" + fileName.Trim() + "\"" + " -T -S " + servername + " -d " + dbinstance + " -b 1000 -c -C 65001 -t~";

                process.Start();
             process.BeginOutputReadLine();
             process.WaitForExit();
             string stderr = process.StandardError.ReadToEnd();
             console.writeline(stderr);

         }

How to capture the error ? Is the above method correct ? Thanks

sql-server-generaldotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered Viorel-1 edited

Consider this approach:

 . . .
 process.Start( );
 process.WaitForExit( );
 int c = process.ExitCode;
 if( c == 0 )
 {
    Console.WriteLine( "Success" );
    . . .
 }
 else
 {
    Console.WriteLine( "Error" );
    . . .
 }

If you want to intercept and analyse the output text, including errors, then remove BeginOutputReadLine and use ‘string text = process.StandardOutput.ReadToEnd( )’.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered ErlandSommarskog commented

In theory, if your argument is okay, the following code should get the correct result.

     static void Main(string[] args)
     {
         using (Process process = new Process())
         {
             ProcessStartInfo startInfo = new ProcessStartInfo();
             startInfo.UseShellExecute = false;
             startInfo.RedirectStandardOutput = true;
             startInfo.RedirectStandardError = true;
             startInfo.FileName = "CMD.exe";
             startInfo.Arguments = "/c bcp -v";
             process.StartInfo = startInfo;
             process.Start();

             string outputString = process.StandardOutput.ReadToEnd();
             string errorString = process.StandardError.ReadToEnd();

             Console.WriteLine(outputString );
             Console.WriteLine(errorString );
             Console.WriteLine();
         } 
                      
         Console.WriteLine("Press any key to continue......");
         Console.ReadLine();
     }

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi.
when I tried the above code, I am getting an error as :

'Cannot mix synchronous and asynchronous operation on process stream.'

0 Votes 0 ·
TimonYang-MSFT avatar image TimonYang-MSFT PadmanabhanVenkatesh-6789 ·

@PadmanabhanVenkatesh-6789
I modified the code, and now I call the bcp command in cmd.
If there is no error, errorString is an empty string, otherwise, outputString is an empty string.

0 Votes 0 ·

In this line:
startInfo.Arguments = "/c bcp -v"

How do i pass my arguments ? is it after -v ?

0 Votes 0 ·
Show more comments