Obtaining provider-agnostic error information from a DbException RRS feed

  • Question

  • I am building something which I hope to be somewhat provider-agnostic (identical data model potentially implemented in any of a number of possible database engines), and need to work with certain error conditions which relate to the data model. For example: duplicate key (as when the application user would attempt to store something which would result in the duplication of a primary key) or foreign key constraint violation (as when the application user would attempt to delete a "parent" row without first deleting any/all child rows.

    So, I have my DbCommand objects created by whatever provider throwing a DbException when such a condition occurs, with, so far as I can tell, only a message string to describe what happened, and expressed in "database-speak" rather than "application-speak". Ideally, I'd like some sort of enumeration, consistent across all data providers, which my application code can use (reliably) to recognize errors such as duplicate keys, foreign key constraint violations, and perhaps a few others - basically data integrity / business logic type rules built into the data model and enforced by the database engine. This would allow the application to select and issue a more "user friendly" error message - expressed in application terms rather than database terms.

    I know I could probably achieve this by catching the specific exception or casting the general DbException to the provider-specific exception, but either way it means coding for each provider as well as statically binding the application to all of those providers, when only one is actually being used.

    Given a DbException, is there any way to get a uniform, platform-agnostic error code (either enumeration or plain integer) which expresses the specific error condition that occurred?

    Thursday, June 13, 2013 9:31 PM


  • It sounds like you are asking for a generic exception that would map to each exception that is implemented by each provider. Since there are an infinite number of provider specific exceptions I don't think this would be possible. That is, not without limiting the content of provider specific exceptions, which would make them practically useless.

    You might want to take a look at the below link to see if it offers a minimal workaround for a common DbException:


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, June 17, 2013 1:22 PM