数据库连接的幻像错误

数据库连接的幻像错误

问题描述:

我有一台ASP/C#(4.0)应用程序在我的机器上运行了好几天。突然,在测试中,我得到了这个错误。在抛出错误之前,该应用似乎“等待”了很短的时间。我能够通过SQL-Management/Web/RDC/Ping访问服务器。我试过iisreset,回收应用程序池,并开始/停止应用程序池。重新启动之前,没有任何东西可以摆脱错误。我假设它与连接池有关,我认为我做错了什么。这个问题也影响了VS开始调试/等的迷你iis。数据库连接的幻像错误

我目前无法再让它发生,但我真的讨厌“修复自己”的问题。由于目前还无法测试,所以我只是在寻找方向,有些想法让它再次出现。

感谢:-)

异常信息: 异常类型:SQLEXCEPTION 异常消息:与SQL Server建立连接时出现与网络相关的或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,并将SQL Server配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开连接到SQL Server) 在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning ) at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo,SqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,Boolean ignoreSniOpenTimeout,TimeoutTimer timeout,SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,String newPassword,Boolean redirectedUserInstance,SqlConnection owningObject,SqlCo在System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity同一性,SqlConnectionString nnectionString connectionOptions,TimeoutTimer超时) 在System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection的owningObject,TimeoutTimer超时,SqlConnectionString connectionOptions,字符串NEWPASSWORD,布尔redirectedUserInstance) connectionOptions,对象providerInfo,字符串NEWPASSWORD,SqlConnection的owningObject,布尔redirectedUserInstance) 在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,对象poolGroupProviderInfo,池类DBConnectionPool,的DbConnection owningConnection) 在System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection( DbConnection owningConnection,DbConnectionPool池,DbConnectionOptions选项) at System.Data.P roviderBase.DbConnectionPool.CreateObject(的DbConnection owningObject) 在System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(的DbConnection owningObject) 在System.Data.ProviderBase.DbConnectionPool.GetConnection(的DbConnection owningObject) 在System.Data.ProviderBase.DbConnectionFactory.GetConnection (的DbConnection owningConnection) 在System.Data.ProviderBase.DbConnectionClosed.OpenConnection(的DbConnection outerConnection,DbConnectionFactory connectionFactory的) 在System.Data.SqlClient.SqlConnection.Open() 在MyApp.Default.BeginAsyncGetState(对象发件人,EventArgs的,的AsyncCallback cb,Object state)in C:... \ Default.aspx.cs:line 65 at System.Web.UI.Page.PageAsyncInfo.CallHandlersPossiblyUnderLock(Boolean onPageThread) at System.We b.UI.Page.PageAsyncInfo.CallHandlersCancellableCallback(Object state) at System.Web.HttpContext.InvokeCancellableCallback(WaitCallback callback,Object state) at System.Web.UI.Page.PageAsyncInfo。CallHandlers(布尔onPageThread)

public partial class Default : System.Web.UI.Page 
    { 

     SqlCommand _sqlCMD; 
     SqlConnection _sqlConn = null; 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      String strStateCode = Request.QueryString["State"] ?? String.Empty; 

      if (!Page.IsPostBack || !(Session["vchCurrentStateID"] ?? String.Empty).Equals(strStateCode)) 
      { 
       AddOnPreRenderCompleteAsync(
          new BeginEventHandler(BeginAsyncGetState), 
          new EndEventHandler(EndAsyncGetState) 
         ); 
      } 
     } 

     protected IAsyncResult BeginAsyncGetState(object sender, EventArgs e, AsyncCallback cb, object state) 
     { 
      String ConString = System.Configuration.ConfigurationManager.ConnectionStrings["ReportServer"].ConnectionString; 
      _sqlConn = new SqlConnection(ConString); 
      _sqlCMD = new SqlCommand(); 
      SqlDataReader myReader = null; 

      _sqlCMD.Connection = _sqlConn; 
      _sqlCMD.CommandType = CommandType.StoredProcedure; 

      String strStateCode = Request.QueryString["State"] ?? String.Empty; 

      IAsyncResult tmpResult = null; 
      try 
      { 
       _sqlConn.Open(); 

       _sqlCMD.CommandText = "dbo.StateLevel_gState"; 
       _sqlCMD.Parameters.AddWithValue("@vchShortCode", strStateCode); 

       tmpResult = _sqlCMD.BeginExecuteReader(cb, state); 
      } 
      catch (Exception ex) 
      { 
       if (_sqlConn != null) 
        _sqlConn.Close(); 
       if (_sqlCMD != null) 
        _sqlCMD.Dispose(); 
       if (myReader != null) 
        myReader.Dispose(); 

       throw; 
      } 
      return tmpResult; 
     } 

     void EndAsyncGetState(IAsyncResult ar) 
     { 
      try 
      { 
       using (SqlDataReader myReader = _sqlCMD.EndExecuteReader(ar)) 
       { 
        if (myReader.Read()) 
        { 
         lblStateName.Text = myReader.GetString(1); 
         Session["iCurrentStateID"] = myReader.GetInt32(0); 
         Session["vchCurrentStateID"] = Request.QueryString["State"]; 
         Session["vchReportLocation"] = myReader.GetString(3); 
        } 
        else 
        { 
         lblStateName.Text = "Invalid State"; 
        } 
       } 
      } 
      finally 
      { 
       if (_sqlConn != null) 
        _sqlConn.Close(); 

       if (_sqlCMD != null) 
       { 
        _sqlCMD.Dispose(); 
       } 
      } 
     } 
     } 

看起来像一个命名管道的问题,它可能是你不知道的数据库服务器的变化。看看这个:http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/

希望它可以帮助

+0

尽管信息丰富,但我不认为该链接适用。它谈到了改变事物的SQL服务器端。我知道服务器工作正常,因为其他计算机连接到它非常好,当我重新启动时问题自行解决。另一个有趣的部分是我的网络应用程序工作得很好,然后在我使用它时突然停止工作。我登录,注销,并试图重新登录,只是迎接这个错误。我仍然给予+1,因为该链接对某人有用。 – Bengie 2012-03-12 15:17:44

命名管道是最后协议的sql提供商如果使用多种协议,所以你可能只是有一个一般的身份验证问题尝试。通过映射驱动器或使用“运行方式”连接到使用不同Windows帐户的服务器时,我已经看到您描述的错误。

可能有效的一个技巧是在etc/hosts文件中为您的服务器创建一个别名,并在连接字符串中使用它。这有助于保持凭据不被跨进程共享。