最近不知道为什么老是报数据库连接超时。
错误信息:error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
根据这几天的测试估计是异步的时候导致连接没有关闭的问题。但是我检查了一些都没有发现。
 public IAsyncResult IAsyncQuery(int id)
        {
            DataBase db = GetDataBase(DataBaseName);   
            using (DbConnection connection = db.CreateConnection())
            {
                connection.Open();
                using (DbCommand command = db.GetSqlStringCommand(base["Select语句"]))
                {
                    db.AddInParameter(command, "id", DbType.Int32, id);
                    return db.ExecuteAsyncDataReader(command, CommandBehavior.CloseConnection);
                }
                connection.Close();
            }
        }
恳请高手赐教。
本人新手没多少分,见谅!!

解决方案 »

  1.   

    为什么我不用异步的没问题呢
     public DataTable  Query(int id)
            {
                DataBase db = GetDataBase(DataBaseName);
                using (DbCommand command = db.GetSqlStringCommand(base["Select"]))
                {
                    db.AddInParameter(command, "id", DbType.Int32, id);
                    return db.ExecuteDataSet(command).Tables[0];
                }
            }
      

  2.   

      using (DbCommand command = db.GetSqlStringCommand(base["Select语句"]))
                    {
                        db.AddInParameter(command, "id", DbType.Int32, id);
                        return db.ExecuteAsyncDataReader(command, CommandBehavior.CloseConnection);
                    }
                    connection.Close();
    在close之前,已经return了,所以这里连接未关闭
      

  3.   

    请问应该怎样放呢!
    using (DbCommand command = db.GetSqlStringCommand(base["Select语句"]))
                    {
                        db.AddInParameter(command, "id", DbType.Int32, id);
                         connection.Close(); 
                        return db.ExecuteAsyncDataReader(command, CommandBehavior.CloseConnection);
                    }
    那这样改可以吗?,为什么using没起到作用呢?