How can I store SQLSERVER's raiserror message in C# ?
问题:
回答1:
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.
回答2:
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.