J
Julie Lerman
hmmm - just remembering that adapter.fill opens and closes the connection
for you. :-(
so I'm lookingmore closely at your code
using (SqlDataAdapter TheAdapter = new SqlDataAdapter("select * from
customermaster", DoodConnection))
{
using (DataSet TheCustomers = new DataSet())
{
TheCustomers.Tables.Add("CustomerMaster");
TheAdapter.Fill(TheCustomers, "CustomerMaster");
TheAdapter.Dispose();
TheCustomers.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
It's very unkosher. First of all, you do not have to call dispose on the
adapter or the dataset. They are managed objects. The issues are around the
connection itself - which is calling unmanaged resources.This is why you
need to be sure to close connections. Again adapeter.fill will implicitly
open and close a connection. The adapter.dispose has nothing at all to do
with it.
Also. the using stuff is again, to deal with the connection resource issues.
In this case it is not really needed for adapter. However, by using the
using statement, IT will dispose stuff for you . So by explicitly calling
dispose on adapter and dataset, you are calling dispose twice which is
unecessary. I don't like the using on the dataadapter and dataset. I'd get
rid of them.
I would start from more typical code, watch the resources and then if you
feel you must do allof that
explicit stuff. And I feel the same as Marina about the GC. It is a very
complicated beast and there are rare occasions when you would override the
default GC behavior.
Lastly - when do you do something with the dataset? you fill it then dispose
it. Aren't you returning it from here?
trial 1
SqlDataAdapter TheAdapter = new SqlDataAdapter("select * from
customermaster",
DoodConnection)
DataSet TheCustomers = new DataSet()
TheCustomers.Tables.Add("CustomerMaster");
TheAdapter.Fill(TheCustomers, "CustomerMaster");
trial2 - just explicitly handling the connection
using (DoodConnection SqlConnection=new SqlConnection(myconnectionstring))
{
SqlDataAdapter TheAdapter = new SqlDataAdapter("select * from
customermaster", DoodConnection)
DataSet TheCustomers = new DataSet()
TheCustomers.Tables.Add("CustomerMaster");
TheAdapter.Fill(TheCustomers, "CustomerMaster");
return TheCustomers; //????
}
for you. :-(
so I'm lookingmore closely at your code
using (SqlDataAdapter TheAdapter = new SqlDataAdapter("select * from
customermaster", DoodConnection))
{
using (DataSet TheCustomers = new DataSet())
{
TheCustomers.Tables.Add("CustomerMaster");
TheAdapter.Fill(TheCustomers, "CustomerMaster");
TheAdapter.Dispose();
TheCustomers.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
It's very unkosher. First of all, you do not have to call dispose on the
adapter or the dataset. They are managed objects. The issues are around the
connection itself - which is calling unmanaged resources.This is why you
need to be sure to close connections. Again adapeter.fill will implicitly
open and close a connection. The adapter.dispose has nothing at all to do
with it.
Also. the using stuff is again, to deal with the connection resource issues.
In this case it is not really needed for adapter. However, by using the
using statement, IT will dispose stuff for you . So by explicitly calling
dispose on adapter and dataset, you are calling dispose twice which is
unecessary. I don't like the using on the dataadapter and dataset. I'd get
rid of them.
I would start from more typical code, watch the resources and then if you
feel you must do allof that
explicit stuff. And I feel the same as Marina about the GC. It is a very
complicated beast and there are rare occasions when you would override the
default GC behavior.
Lastly - when do you do something with the dataset? you fill it then dispose
it. Aren't you returning it from here?
trial 1
SqlDataAdapter TheAdapter = new SqlDataAdapter("select * from
customermaster",
DoodConnection)
DataSet TheCustomers = new DataSet()
TheCustomers.Tables.Add("CustomerMaster");
TheAdapter.Fill(TheCustomers, "CustomerMaster");
trial2 - just explicitly handling the connection
using (DoodConnection SqlConnection=new SqlConnection(myconnectionstring))
{
SqlDataAdapter TheAdapter = new SqlDataAdapter("select * from
customermaster", DoodConnection)
DataSet TheCustomers = new DataSet()
TheCustomers.Tables.Add("CustomerMaster");
TheAdapter.Fill(TheCustomers, "CustomerMaster");
return TheCustomers; //????
}