Search code examples
c#mysqlado.net

How do I retrieve more information about this bulk load error?


I'm attempting to bulk-load some data into a MySql server, using the MySqlBulkCopy class from the MySqlConnector library.

For the first table it works just fine. For the second table, it errors out, saying 0 out of 10 rows were written. (Using a small data set for testing purposes; real-world usage would involve a lot more rows than that.)

Debugging into the library code, I find that the warning count from the operation's result is 30, and the affected row count is 0, but the library doesn't do anything with those warnings. They're never surfaced to the user-facing error message, and with the way the reply packet is basically just metadata, I suspect that some additional API call would be needed to retrieve the warnings.

Assuming that I have full access to both the MySql server and the source of the library, what do I have to do to read these warnings and figure out what's going wrong with my bulk load operation?

(Note: Please don't suggest "solving" this by switching to the other MySql ADO.NET driver. For my use case, that would just be trading one problem for another, potentially worse one.)


Solution

  • There are two approaches:

    SHOW WARNINGS

    After MySqlBulkCopy.WriteToServer finishes, call ExecuteReader for a SHOW WARNINGS command. As per the documentation this will "[display] information about the conditions resulting from execution of the most recent nondiagnostic statement in the current session".

    InfoMessage event handler

    Before executing MySqlBulkCopy.WriteToServer, attach an event handler to MySqlConnection.InfoMessage. During WriteToServer, the event handler will be called with an MySqlInfoMessageEventArgs object. This contains an Errors property that contains a list of errors and warnings that have occurred.