Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing SqlServer's raiserror message in C#

How can I store SQLSERVER's raiserror message in C# ?

like image 500
priyanka.sarkar Avatar asked Mar 09 '26 15:03

priyanka.sarkar


2 Answers

It depends on the severity of your RAISERROR. Severities 1-10 are considered information messages and do not break the flow of your C# client, ie. they don't throw exception. These information messages will trigger the InfoMessage event on your connection.

If you RAISERROR with severity between 10 and 16 is considered an error and your SqlCommand.Execute will throw and exception you can catch.

Severities above 17 are not for you to play with, they are only to be used by the engine to indicate severe problems that may take the database offline or shutdown the instance. Is not technically possible to raise errors with severity above 25, your example with severity 100 is just silliness.

like image 104
Remus Rusanu Avatar answered Mar 12 '26 03:03

Remus Rusanu


Have a look at:

http://www.sommarskog.se/error-handling-I.html#ADO.Net

In particular: the SqlClient section. The SqlError class gives you all the information you need.

like image 42
Sam Saffron Avatar answered Mar 12 '26 04:03

Sam Saffron