SQLDataReader 和 CommandBehaviour.CloseConnection
我有一个名为“db”的通用类,它直接与数据库对话。并有一个名为“ExecuteDataReader”的方法,如下所示:
public SqlDataReader ExecuteDataReader(SqlCommand cmd)
{
try
{
OpenConnection();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
catch (Exception ex)
{
Utils.Debug(string.Format("Err in {0}.{1} : {2}\nSQL : {3}", this.GetType(), "ExecuteDataReader", ex.Message, cmd.CommandText));
return null;
}
}
然后,我执行资源密集型查询,该查询循环遍历 10000 条父记录和 20000 条子记录以在数据库中更新。然后出现以下错误:
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.
为了解决这些问题,我必须在执行后显式调用 dr.Close() 。
static void ProcessAssessmentCriteria(string UnitReference)
{
SqlCommand cmd = new SqlCommand("TRACKING.DBTool_GetUniqueAssessmentCriteriaByUnitReference");
cmd.Parameters.Add("@UnitReference", SqlDbType.VarChar, 20).Value = UnitReference;
SqlDataReader dr = db.ExecuteDataReader(cmd);
if (dr.HasRows)
{
while (dr.Read())
{
ProcessDetailAssessmentCriteria(UnitReference, dr["AssessmentRefNumber"].ToString());
Console.WriteLine("---------------");
}
}
dr.Close();
}
据我所知,CommandBehaviour.CloseConnection()会自动关闭连接。不过现在好像还没有关门。您能赐教一下吗?谢谢。
I have a generic class called "db" which talks directly to the database. And have a method called "ExecuteDataReader" as below:
public SqlDataReader ExecuteDataReader(SqlCommand cmd)
{
try
{
OpenConnection();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
catch (Exception ex)
{
Utils.Debug(string.Format("Err in {0}.{1} : {2}\nSQL : {3}", this.GetType(), "ExecuteDataReader", ex.Message, cmd.CommandText));
return null;
}
}
Then, I execute the resource intensive query which loops through 10000 parent records and 20000 child records to update in the database. And then I got the following error:
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.
To solve these problems, I have to call dr.Close() explicitly after the execution.
static void ProcessAssessmentCriteria(string UnitReference)
{
SqlCommand cmd = new SqlCommand("TRACKING.DBTool_GetUniqueAssessmentCriteriaByUnitReference");
cmd.Parameters.Add("@UnitReference", SqlDbType.VarChar, 20).Value = UnitReference;
SqlDataReader dr = db.ExecuteDataReader(cmd);
if (dr.HasRows)
{
while (dr.Read())
{
ProcessDetailAssessmentCriteria(UnitReference, dr["AssessmentRefNumber"].ToString());
Console.WriteLine("---------------");
}
}
dr.Close();
}
Due to my knowledge, the CommandBehaviour.CloseConnection() automatically close the connection. But it seems that it does not close now. Could you please enlighten me? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CommandBehavior.CloseConnection 来自 MSDN
因此,只有当您关闭 DataReader 时,连接才会关闭。
CommandBehavior.CloseConnection from MSDN
So only when you close the DataReader the connection is Closed.