SQLDataReader 和 CommandBehaviour.CloseConnection

发布于 2024-11-29 04:44:14 字数 1833 浏览 0 评论 0原文

我有一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2024-12-06 04:44:15

CommandBehavior.CloseConnection 来自 MSDN

当命令执行时,关联的Connection对象是
当关联的 DataReader 对象关闭时关闭。

因此,只有当您关闭 DataReader 时,连接才会关闭。

CommandBehavior.CloseConnection from MSDN

When the command is executed, the associated Connection object is
closed when the associated DataReader object is closed.

So only when you close the DataReader the connection is Closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文