views:

16

answers:

0

While trying to test an invalid connection string against a SQL Server 2008 Express instance, I found this weird behavior: specifying an invalid Initial Catalog raises an SQLException whose Number is sometimes 233, and sometimes 18456.

Code may illustrate it better.

// The following connection string has a purposely incorrect initial catalog:
string invalidConnString = @"Data Source=.\SQLEXPRESS;Initial Catalog=INVALID_DATABASE_NAME;User Id=dummyUser;Password=dummyPassw;";

SqlConnection connection = new SqlConnection(invalidConnString);

try
{
    connection.Open();
}
catch (SqlException sex)
{
    Console.WriteLine(sex.Number); // I "randomly" get either 233 or 18456
    throw;
}
finally
{
    connection.Close();
}

The system error codes from the Books Online specify that

  • 233 - A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)
  • 18456 - Login failed for user '%.*ls'.%.*ls

I presume both are different ways to say: unsuccessful login. But then, why isn't the failure consistent?