【问题标题】:SQL Server connection arbitrarily returns 233 or 18456 error codesSQL Server连接任意返回233或18456错误码
【发布时间】:2011-03-29 06:10:39
【问题描述】:

在尝试针对 SQL Server 2008 Express 实例测试无效连接字符串时,我发现了这种奇怪的行为:指定无效的 Initial Catalog 会引发 SQLException,其编号有时为 233,有时为 18456。

代码可以更好地说明它。

// 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();
}

联机丛书中的system error codes 指定

  • 233 - 与服务器建立连接成功,但在登录过程中出现错误。 (提供者:共享内存提供者,错误:0 - 管道的另一端没有进程。)
  • 18456 - 用户 '%.*ls'.%.*ls 登录失败

我认为两者都是不同的说法:登录失败。但是,为什么失败不一致呢?

【问题讨论】:

    标签: sql-server ado.net


    【解决方案1】:

    温贝托,

    这是使用 SQL Express 时非常常见的问题,根本原因是 SQL Express 默认打开了 AUTO_CLOSE 数据库选项。当数据库的所有用户关闭时,数据库被关闭并干净地关闭。当用户下次登录时,如果数据库重新打开,将无法快速验证用户对该数据库的权限。

    AUTO_CLOSE 还有其他副作用,它会刷新过程缓存,通常会导致更高的 CPU 成本。

    below 命令是你的朋友。

    ALTER DATABASE DBNAME SET AUTO_CLOSE OFF
    

    http://msdn.microsoft.com/en-us/library/bb522682.aspx

    【讨论】:

    • 非常好!即使我不再参与那个原始项目,我也会接受你的回答。谢谢!
    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2016-10-12
    • 2015-07-25
    • 1970-01-01
    相关资源
    最近更新 更多