views:

54

answers:

2

I really want to do something useful when a PK violation occurs but I hate trapping error numbers... they just don't read right without comments (they're certainly not self documenting).

I know I can find all the potential error numbers at SQL Server books online but I really want to be able to pass the error number to some helper class or look it up against a Dictionary of some sort rather than have non-descript err numbers everywhere.

Has anyone got / seen any code anywhere that encapsulates the SQL Server Error numbers in this way as I don't want to re-invent the wheel (or I'm lazy maybe).

A: 

If we all work on this project, we will soon have an enumeration to use: http://sqlenum.codeplex.com/

Stefan
That project hasn't been updated since 2008 and has exactly 0 error messages in it so far.
Ben Robinson
Thats why I write about the project, lets do something about it. (And its not complete true, it has ONE defined message in it. hehe..)
Stefan
+2  A: 

If you use TRY/CATCH in SQL Server then when you rethrow the error it's always 50000 anyway.

Where are the error numbers coming from? LINQ? ORM? Embedded SQL? Stored procs?

gbn
Sproc. No try catch in sql. Try catch in C# - error number is in this instance 2601 but I'd like to catch others in the future. Is it better to use try catch in sql and use RAISERROR to generate custom errors for only those I'm genuinely interested in... maybe my problem is in my thinking about which domain the logic should be in?
Mr Grok
Why not enumerate sys.messages then? In conjunction with the error text that has been filled in with the details.
gbn
For the sake of completeness:In the end I decided to raise my own error in my sproc when something expected but not fatal happened and later enumerate the SqlError collection (ex.Errors) in C#. At which point, when the error is 50000, I add the error message to a string and return that as the Message part of my custom Exception along with the original exception as the innerException.
Mr Grok