I have a question, and it is not a problem. My database have some restrictions, as unique names (Column that stores the name of something that must be unique) When inserting or updating, the db verifies if we are not overriding some name, and if so, returns an error that is convertable to SqlException with code 2627
My Question is... if exists some table or something that I can consult to indicate to the final user what to do?
In this case.. that exists a user with the same name....
I think, you should take a look at SqlExceptions Errors property and take a look at errors number which should point you to a message in master.dbo.sysmessages table.
Don't assume a specific error code always refers to the same problem.
select * from master.dbo.sysmessages where description like '%timeout%'
This will give you all the following possible error messages - all with code 1033
:
The value provided for the timeout is not valid. Timeout must be a valid integer between 0 and 2147483647. 1033
Timeout occurred while waiting for latch: class '%ls', id %p, type %d, Task 0x%p : %d, waittime %d seconds, flags 0x%I64x, owning task 0x%p. Continuing to wait. 1033
Communications to the remote server instance '%.*ls' failed to complete before its timeout. The ALTER DATABASE command may have not completed. Retry the command. 1033
Database mirroring timeout value %d exceeds the maximum value 32767. 1033 Metadata cache entry %d:%d in database ID(%d) is not checked for coherency due to lock timeout. 1033
The marked transaction "%.*ls" failed. A timeout occurred while attempting to place a mark in the log by committing the marked transaction. This can be caused by contention with Microsoft Distributed Transaction Coordinator (MS DTC) transactions or other 1033
The Bulk Insert operation of SQL Server Destination has timed out. Please consider increasing the value of Timeout property on the SQL Server Destination in the dataflow. 1033
...and many many more errors in all languages.
So it's safe to assume 1033
means some kind of timeout? Unfortunately even that is not true:
select description from master.dbo.sysmessages where msglangid=1033
Warning: Fatal error %d occurred at %S_DATE. Note the error and time, and contact your system administrator.
Query not allowed in Waitfor.
Incorrect syntax near '%.*ls'.
The %S_MSG that starts with '%.*ls' is too long. Maximum length is %d. ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
The ORDER BY position number %ld is out of range of the number of items in the select list.
and many many more non-timeout related errors.
So I'm sticking with :
IsSqlTimeout = ((ex as SqlException).Message.ToUpper().Contains("TIMEOUT");
So to actually answer your question - it depends on what your user actually is capable of understanding or should be told about.
I would suggest you log all errors (codes and message) over a period of time, group them and see which are most common to your environment.
来源:https://stackoverflow.com/questions/10531369/sqlexception-errorcodes