Search code examples
sqlsql-serverversionsqlexception

Is System.Data.SqlClient.SqlException.Number the same in all version's of SQL Server?


I want to categorize the SQL Exception only for Login failed for user 'username' message on catch block. So I tried this way.

   if(exception.Number == 18456)
       return false;
   else
       throw exception;

I want to throw all other exception other than login failed.

Is the Number I am using here the same for this error text in all version's of Sql Server?


Solution

  • The SQL Server error codes don't change - once defined they stay the same, new error codes might be added with each version, but existing ones stay the same.

    See this link for a list of all SQL Server error codes as of version 2008 R2.

    Those messages (or message templates, with placeholders) are surfaced in the sys.messages system catalog view.