连接池暴蹦....
在运行程序的时候:
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.....