2012-03-08 126 views
1

我有一臺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(); 
       } 
      } 
     } 
     } 

回答

1

看起來像一個命名管道的問題,它可能是你不知道的數據庫服務器的變化。看看這個: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

0

命名管道是最後協議的sql提供商如果使用多種協議,所以你可能只是有一個一般的身份驗證問題嘗試。通過映射驅動器或使用「運行方式」連接到使用不同Windows帳戶的服務器時,我已經看到您描述的錯誤。

可能有效的一個技巧是在etc/hosts文件中爲您的服務器創建一個別名,並在連接字符串中使用它。這有助於保持憑據不被跨進程共享。