关于数据库连接池得问题 (reader究竟该不该关闭!我做了测试,怎么关闭不关闭都是一样得?希望高手解答)
最近在研究数据库连接池得问题,看了一些资料后。些了一个代码测试。
代码如下
private SqlDataReader DoSomeThing()
{
string connectstring = System.Configuration.ConfigurationSettings.AppSettings["test"].ToString();
SqlDataReader rd ;
this.Label1.Text="数据库已经打开!连接字符串是"+connectstring;
SqlConnection conn = new SqlConnection(connectstring);
SqlCommand cmd = new SqlCommand("select * from Life_Bdt_Class",conn);
try
{
conn.Open();
rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rd;
}
catch(SqlException Ex)
{
Response.Write(Ex.Errors.ToString());
return null;
}
finally
{
// conn.Close();
}
}
然后 用这段代码调用:
private void Button1_Click(object sender, System.EventArgs e)
{
SqlDataReader dt = null;
for (int i = 1;i<1000;i++)
{
dt = DoSomeThing();
if (dt != null)
{
this.Label1.Text="下面是查询的结果◎!";
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
dt.Close();
}
else
{
this.Label1.Text="出错了!! 还不快检查检查!!";
}
}
}
但是很奇怪 我在调用代码中“dt.Close();”这句,写不写好像都无所谓,程序都不报错,按理说系统默认得连接池应该最大是100,但是我在循环中调用得次数远远得大于这个最大值,为甚么我不关闭reader却没有问题呢??
希望高手指点