DB连接的幻像错误

时间:2012-03-08 17:02:55

标签: c# asp.net connection-pooling

我有一个ASP / C#(4.0)应用程序已在我的机器上运行了好几天。突然,在测试过程中,我收到了这个错误。在抛出错误之前,应用程序似乎确实“等待”了一小段时间。我能够通过SQL-Management / Web / RDC / Ping访问服务器就好了。我尝试了iisreset,回收应用程序池,以及启动/停止应用程序池。在重新启动之前,没有任何东西可以消除错误。我假设它与连接池有关,我想我做错了什么。这个问题也影响了VS为调试/等启动的迷你问题。

我目前无法让它再次发生,但我真的很讨厌“自我修复”的问题。由于目前无法对此进行测试,我只是在寻找方向和一些想法,因为它再次出现。

谢谢: - )

异常信息:     异常类型:SqlException     异常消息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接)    在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)    在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()    在System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo,SqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity)    在System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,Boolean ignoreSniOpenTimeout,TimeoutTimer timeout,SqlConnection owningObject)    在System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,String newPassword,Boolean redirectedUserInstance,SqlConnection owningObject,SqlConnectionString connectionOptions,TimeoutTimer timeout)    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject,TimeoutTimer timeout,SqlConnectionString connectionOptions,String newPassword,Boolean redirectedUserInstance)    在System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity标识,SqlConnectionString connectionOptions,Object providerInfo,String newPassword,SqlConnection owningObject,Boolean redirectedUserInstance)    在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,Object poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection)    在System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection,DbConnectionPool池,DbConnectionOptions选项)    在System.Data.ProviderBase.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()    在C:... \ Default.aspx.cs中的MyApp.Default.BeginAsyncGetState(Object sender,EventArgs e,AsyncCallback cb,Object state):第65行    在System.Web.UI.Page.PageAsyncInfo.CallHandlersPossiblyUnderLock(Boolean onPageThread)    在System.Web.UI.Page.PageAsyncInfo.CallHandlersCancellableCallback(对象状态)    在System.Web.HttpContext.InvokeCancellableCallback(WaitCallback回调,对象状态)    在System.Web.UI.Page.PageAsyncInfo.CallHandlers(Boolean 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();
                }
            }
        }
        }

2 个答案:

答案 0 :(得分: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/

希望有所帮助

答案 1 :(得分:0)

命名管道是sql提供程序在使用多个协议时尝试的最后一个协议,因此您可能只有一般身份验证问题。我通过映射驱动器或使用"运行为"来连接到使用不同Windows帐户的服务器时发生的错误。

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