1. 程式人生 > >Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This ma

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This ma

連線池暴蹦....
在執行程式的時候:

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 SqlDataReader ddlbind()
    {
        string sqlstr = "select distinct case  flag when   'N' then '離職' when 'Y' then '在職' END  as Workflag from Flag";
         SqlConnection sqlcon = new SqlConnection(PublicVar.strCon_SHATDB);
         SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);

         sqlcon.Open();
        return sqlcom.ExecuteReader();
    }

    public SqlDataReader ddlbinddepat()
    {
        string sqlstr = "select description from Dept where DeptCode in('PRD_2','PRD_3')";
        SqlConnection sqlcon = new SqlConnection(PublicVar.strCon_SHATDB);
        SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        return sqlcom.ExecuteReader();
    }

沒有關閉資料庫的連線? 就趕緊寫了一個關閉的sqlcon.Close(); 執行沒有效果,很簡單都已經返回SqlDataReader 了(當然不行)
於是想到用一個SqlDataReader 的物件來接收
        SqlDataReader dr = sqlcom.ExecuteReader();
        sqlcon.Close();
        return dr;
是關閉了,但沒有bind的資料沒有了,因為關閉了.
........................
感覺返回SqlDataReader 很耗資源,既然此路不通,我走返回datetable,故在資料訪問類裡面新增一個
函式
  public static DataTable Query(string strSql)
        {
            Open();
            using (SqlDataAdapter dap = new SqlDataAdapter(strSql, MyConnection))
            {
                using (DataTable dt = new DataTable())
                {
                    dap.Fill(dt);
                    Close();
                    return dt.DefaultView.Table;
                }
            }
        }
    public DataTable ddlbind()
    {
        string sqlstr = "select distinct case  flag when   'N' then '離職' when 'Y' then '在職' END  as Workflag from Flag";
        return DataAccess.DataAccessSHDB.Query(sqlstr);
    }
    public DataTable ddlbinddepat()
    {
        string sqlstr = "select description from Dept where DeptCode in('PRD_2','PRD_3')";
        return DataAccess.DataAccessSHDB.Query(sqlstr);
    }

除錯OK ....未完

原因何在?
using new.....

1348118583_4652.jpg