在将控件的数据源设置为sqldatareader时,是否需要使用using语句

时间:2022-09-02 13:49:06

I know it is recommended to use a using statement with a sqldatareader, but I was wondering if it is necessary when you are setting the datasource of a control to a datasource directly.

我知道建议使用带有sqldatareader的using语句,但是我想知道,当您将控件的数据源直接设置为数据源时,是否有必要这样做。

in some places in my code I do this.....

在我代码中的一些地方,我做这个……

using (SqlDataReader reader = getReader())
{
    while (reader.Read())
    {
         // do stuff with data
    }
}

when binding to a control I do this.....

当绑定到控件时,我做这个……

ddlCustomer.DataSource = getReader();  // get Reader returns a sqldataReader

ddlCustomer.DataBind();

In the second case, do I need to use a using statement. Do i have to first declare an SqlDataReader in a using statement, then set the DataSource to that object. Seems like more clutter in the code, so i was hoping binding to the SqlDataReader disploses of the SqlDataReader.

在第二种情况下,是否需要使用using语句。我是否必须首先在using语句中声明SqlDataReader,然后将数据源设置为该对象。看起来代码中更杂乱,所以我希望绑定到SqlDataReader的SqlDataReader分配器。

Thanks

谢谢

3 个解决方案

#1


2  

At some point, the Dispose() method on the DataReader needs to be called. The finalizer will do it for you sometime in the future, but that's probably to late.

在某个时刻,需要调用DataReader上的Dispose()方法。终结者会在将来的某个时候为你做这件事,但那可能已经太晚了。

So, the question becomes, "If I don't, who will?"

所以,问题变成了,“如果我不这样,谁会呢?”

There's a chance that the DataBind() method will recognize that the object assigned to the DataSource property is an IDisposable, and call Dispose when it's finished Databinding, but I wouldn't count on it.

DataBind()方法可能会识别分配给DataSource属性的对象是IDisposable,并在完成数据库之后调用Dispose,但我不会指望它。

So, yes, I'd go with the using{}.

所以,是的,我选择使用{}。

using (SqlDataReader reader = getReader()) 
{ 
    ddlCustomer.DataSource = reader;
    ddlCustomer.DataBind(); 
} 

#2


1  

If I'm reading your question right, yes you would have to declare the SqlDataReader in the using statement:

如果我正确地阅读了您的问题,那么您必须在using语句中声明SqlDataReader:

using (SqlDataReader reader = getReader())
{
    ddlCustomer.DataSource = reader;  // get Reader returns a sqldataReader
    ddlCustomer.DataBind();
}

Binding the reader does not dispose it explicitly. If you want to do that, you can refactor this into a single method to bind a reader to a control:

绑定阅读器不会显式地处理它。如果您想这样做,您可以将其重构为一个单独的方法来将阅读器绑定到控件:

public void BindControl(Control Ctl, SqlDataReader reader)
   {
        using (reader)
        {
          ddlCustomer.DataSource = reader;  // get Reader returns a sqldataReader
          ddlCustomer.DataBind();
        }
   }

#3


0  

BaseDataBoundControl makes no promise that it disposes of its data source, so callers must take responsibility themselves.

BaseDataBoundControl不能保证它会处理它的数据源,因此调用者必须自己承担责任。

#1


2  

At some point, the Dispose() method on the DataReader needs to be called. The finalizer will do it for you sometime in the future, but that's probably to late.

在某个时刻,需要调用DataReader上的Dispose()方法。终结者会在将来的某个时候为你做这件事,但那可能已经太晚了。

So, the question becomes, "If I don't, who will?"

所以,问题变成了,“如果我不这样,谁会呢?”

There's a chance that the DataBind() method will recognize that the object assigned to the DataSource property is an IDisposable, and call Dispose when it's finished Databinding, but I wouldn't count on it.

DataBind()方法可能会识别分配给DataSource属性的对象是IDisposable,并在完成数据库之后调用Dispose,但我不会指望它。

So, yes, I'd go with the using{}.

所以,是的,我选择使用{}。

using (SqlDataReader reader = getReader()) 
{ 
    ddlCustomer.DataSource = reader;
    ddlCustomer.DataBind(); 
} 

#2


1  

If I'm reading your question right, yes you would have to declare the SqlDataReader in the using statement:

如果我正确地阅读了您的问题,那么您必须在using语句中声明SqlDataReader:

using (SqlDataReader reader = getReader())
{
    ddlCustomer.DataSource = reader;  // get Reader returns a sqldataReader
    ddlCustomer.DataBind();
}

Binding the reader does not dispose it explicitly. If you want to do that, you can refactor this into a single method to bind a reader to a control:

绑定阅读器不会显式地处理它。如果您想这样做,您可以将其重构为一个单独的方法来将阅读器绑定到控件:

public void BindControl(Control Ctl, SqlDataReader reader)
   {
        using (reader)
        {
          ddlCustomer.DataSource = reader;  // get Reader returns a sqldataReader
          ddlCustomer.DataBind();
        }
   }

#3


0  

BaseDataBoundControl makes no promise that it disposes of its data source, so callers must take responsibility themselves.

BaseDataBoundControl不能保证它会处理它的数据源,因此调用者必须自己承担责任。