Share via

BCP not returning any rows

Padmanabhan, Venkatesh 246 Reputation points
2021-05-19T16:54:21.7+00:00

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 | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2021-05-19T18:42:57.77+00:00

    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( )’.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. Timon Yang-MSFT 9,611 Reputation points
    2021-05-20T06:37:39.383+00:00

    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.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.