Will putting a “using” statement around a DataReader close it?

后端 未结 4 1463
栀梦
栀梦 2020-12-18 17:52

I usually write my DataReader code like this:

try
{
    dr = cmd.ExecuteReader(CommandBehavior.SingleResult);
    while (dr.Read())
    {
               


        
4条回答
  •  被撕碎了的回忆
    2020-12-18 18:20

    Typically, using() calls Dispose() and that calls close() in turn.

    In case of a DataReader, the Close is called only when CommandBehavior.CloseConnection is set (see comments of this article http://weblogs.asp.net/joseguay/archive/2008/07/22/ensure-proper-closure-amp-disposal-of-a-datareader.aspx).

    EDIT: This article says something interesting:

    The Close() method on the SqlDataReader calls an InternalClose() method, which does not call Dispose. Note that previously we stated the correct way to do this was to have your close call dispose. To make it even more confusing the Dispose() method actually calls the Close() method so for this object the order is reversed.

提交回复
热议问题