Finer granularity of SQL exceptions?

In C # Is there a way to get finer granularity with SQL exceptions?

I know that a lot can go wrong, but I want to deal with certain cases differently, and parsing the error message doesn't seem very elegant.

Are the error messages generated by the framework also, or are they specific to db?

For example, if I have a primary key violation in INSERT, the error message will always be:

Violation of PRIMARY KEY "PK_tblProduct" restriction. Cannot insert duplicate key into object 'dbo.tblProduct'.

or is it a specific SQLServer.

Update

It looks like I'm after the error number, any idea where I can get the various error numbers?

The best I can do with a quick google: 18456: login failure 18488: Password complete

+3
source share
5 answers

If you catch SqlException, you should have access to .Number, which gives you the SQL error number; this is probably your best approach ... (you can get the same from the event InfoMessage, as well as for non-fatal errors).

Of course, this will not distinguish between tables - you could argue that if you need this level of detail, you should first check (in a serialized isolation level, ideally with UPDLOCK), creating your own error if there is conflicting data.

+1
source

You can find the error codes here.

+1
source

.Number SQLException, , , . , :

  • 17: SQL Server .
  • 4060:
  • 18456:
  • 547:
  • 1205: DeadLock
  • 2627, 2601: / Constriant.
+1

SQL Server , , sys.messages

BOL.

0

: . SQL, . SQL, .

0

Source: https://habr.com/ru/post/1699099/


All Articles