none
How to capture BCP error in .net application RRS feed

  • Question

  • Hi All,

    I am calling BCP.exe from my .net appliation to export data and write file on hard disk. Code moves smoothly even if there is error in BCP while exporting file and even after successful execution of .net application there is no file exported on the disk. I am using System.Diagnostics.ProcessStartInfo and System.Diagnostics.Process classes to call BCP.exe. I tried setting RedirectStandardOutput property of ProcessStartInfo class and capture it through StandardError poperty of Process class but it returns blank. Then I tried following:

    Set RedirectStandardOutput property of ProcessStartInfo class to True and written following code

    System.IO.StreamReader outputStream = proc.StandardOutput;
                string processOutput = outputStream.ReadToEnd();
                if (processOutput.Contains("Error"))
                    throw new Exception(processOutput);

    This code works fine. The only problem with this approach is it cannot differenciate between error and warning as output string contains word "Error" in case warning. I want application not to throw any exception in case of warnings.

    Can someone help me with this? Thanks in advance !!


    Technology always changes, Logic remains same :)

    Sunday, July 29, 2012 6:44 AM

Answers

  • According to the strings you posted, there seems to be some space before the beginning of the message. The following pattern seems to distinguish correctly between error and warning as posted.

    Regex.IsMatch (processOutput, @"^\s*Error(?!.*Warning)", RegexOptions.Multiline)

    HTH
    --mc

    Tuesday, July 31, 2012 9:43 AM

All replies

  • Hi,

    I'm not familiar with the output of BCP, but assuming a warning would contain the word "Warning" somewhere on the first line of the message, you could use a regular expression to weed these out.

          if (Regex.IsMatch (processOutput, @"^Error(?!.*Warning)", RegexOptions.Multiline)) {
            throw new Exception (...);
          }

    The code above will throw an exception only if it finds a line beginning with "Error" that doesn't also contain the word "Warning". Here I'm assuming that the words are spelled and cased exactly that way, and that the word Error appears starting at the very beginning of the line. If that's not the case, and you need help fixing the pattern, please post a few sample outputs.

    HTH
    --mc


    Sunday, July 29, 2012 10:00 AM
  • There are two seperate outputs (ports) from a program (standardout, standarderror).  If your program produces standarderror on a 2nd port you must read the standarderror by connecting another stream to the 2nd port (or use read method instead of a stream).

    jdweng

    Sunday, July 29, 2012 11:21 AM
  • Here are some output samples

    Error:

    SQLState = HY000, NativeError = 0
    Error = [Microsoft][SQL Native Client]The number of fields provided for bcp operation is less than the number of columns on the server.

    Warning:

    Starting copy...
    SQLState = S1000, NativeError = 0
    Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Warning: Server data (8 bytes) exceeds host-file field length (3 bytes) for field (2).  Use prefix length, termination string, or a larger host-file field size.  Truncation cannot occur for BCP output files.

    1 rows copied.
    Network packet size (bytes): 4096
    Clock Time (ms.): total       63


    Technology always changes, Logic remains same :)

    Monday, July 30, 2012 3:45 PM
  • Did you try reading the standerror output?  The code may be a lot simplier if the errors were being reported on standard error.  This way you don't have to parse the standard output messages to determine which messages are errors and which messages aren't errors.

    jdweng

    Monday, July 30, 2012 4:11 PM
  • Well, thats the whole point. StandardError does not return anything even in case of error. I have tried enabling "EnableRaisingEvents" property of Process class and see if I can get something on StandardError but not luck. Moreover, SQL version is 8.0 i.e SQL 2000 and .NET VS 2008 V3.5. Do you think if this is something can cause problem?


    Technology always changes, Logic remains same :)

    Tuesday, July 31, 2012 7:39 AM
  • According to the strings you posted, there seems to be some space before the beginning of the message. The following pattern seems to distinguish correctly between error and warning as posted.

    Regex.IsMatch (processOutput, @"^\s*Error(?!.*Warning)", RegexOptions.Multiline)

    HTH
    --mc

    Tuesday, July 31, 2012 9:43 AM