In Microsoft.Data.SqlClient/System.Data.SqlClient libraries, the Sql errors returned with the SqlException have a property called Number.
In many of StackOverflow questions and other discussions over the internet people suggest to use this property to catch specific issues related to the exception (e.g. deadlock, duplicate keys, etc.).
However, based on the official Microsoft documentation this property can be populated both from the server error code (SQL Server's master.dbo.sysmessages table), Win32 error code and can have some other numbers too (e.g. -2 for timeout). Does this mean that the same number can mean different thing based on the fact where the exception is being thrown from?
An example for this is number 10054.
- Based on the data on the internet, this is being thrown when there is a connecitivy issue - https://dba.stackexchange.com/questions/329332/unable-connect-to-sql-server-on-ssms-and-got-error-10054
- Based on what I see in the SQL Server's master.dbo.sysmessages table, number 10054 corresponds to "The data value for one or more columns overflowed the type used by the provider.".
Another example is 233.
- Based on the data on the internet, this also is being thrown when there is a connectivity issue - SQL Server connection arbitrarily returns 233 or 18456 error codes
- Based on what I see in the SQL Server's master.dbo.sysmessages table, number 233 corresponds to "The column '%.*ls' in table '%.*ls' cannot be null."
So, I want to understand why everyone on the internet is suggesting to use this number to assume what the exact SQL exception is and how safe it is when the same number can mean different things?