ASP.NET: Should I close the connection immediately after the adapter has populated the data set?

I use GridView and I wonder if it’s better for me to close the connection IMMEDIATELY after

adapter.Fill(ds);

or I have to wait until I do:

GridView.DataSource = ds;
GridView.DataBind();

I assume that once the data set is full, I no longer need a connection. I am wrong?

+3
source share
4 answers

When called, adapter.Fill(ds);data is loaded into memory, and you can close the connection immediately after this statement. After you set ds as DataSource in gridview, it will bind data from memory.

Take a look at this article for an understanding of working with disabled data - DataSet and SqlDataAdapter

+4
source

Fill() . Offtopic: , using IDisposable (, SqlConnection), Close().

+3

SqlDataAdapter connection DataSet, - data manipulation , , , finally.

+1

SqlConnection , DataSet .

DataSet ADO.NET . DataReader, DataSet , . , DataSet , DataAdapter (, SqlDataAdapter) SqlConnection. SqlDataAdapter DataSet SqlCommand SqlConnection, DataSet

string sSQL = "SELECT * FROM Products";
string sConnString = 
    "Server=(local);Database=Northwind;Integrated Security=SSPI;";
SqlDataAdapter oDa = new SqlDataAdapter();
DataSet oDs = new DataSet();
using(SqlConnection oCn = new SqlConnection(sConnString))
{
    SqlCommand oSelCmd = new SqlCommand(sSQL, oCn);
    oSelCmd.CommandType = CommandType.Text;
    oDa.SelectCommand = oSelCmd;
    oDa.Fill(oDs, "Products");
}

: ADO.NET DataReader DataSet

-1

All Articles