I need a using statement when installing a control data source on sqldatareader

I know that it is recommended to use the using statement with sqldatareader, but I was wondering if this is necessary when you directly set the control's data source to the data source.

in some places of 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, I need to use the using statement. Should I first declare SqlDataReader in the using statement, and then set the DataSource for this object. It seems that the code is more of a mess, so I was hoping to bind the SqlDataReader preemptions to the SqlDataReader.

thank

+3
source share
3 answers

- Dispose() DataReader. - , , , .

, : " , ?"

, DataBind() , , DataSource, IDisposable Dispose, , .

, , using{}.

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

, , SqlDataReader using:

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

. , , :

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

Source: https://habr.com/ru/post/1761581/


All Articles