Closing of OleDbConnection and OleDbDataReader

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

Hi,

if have this method in a cs file that connections to a
database and returns a datareader to my web form.

public OleDbDataReader GetDataReader(string theSQL)
{
theConn = new OleDbConnection(ConnectionString);
theConn.Open();
OleDbCommand cmd = new OleDbCommand(theSQL, theConn);
OleDbDataReader theResult = cmd.ExecuteReader
(CommandBehavior.CloseConnection);
return theResult;
}

in the aspx file, after getting the result of this Query
method, i will close the datareader. Is this adequate? do
i need to close the OleDbConnection as well? if so, how
can i point to the OleDbConnection?
I realise that my database has got a series of open
connection which i suspect is caused by my web application.

thanks for any help
 
hi,
I've had similar problems where connections that I've
expected to be closed implicitly by asp.net remain open.
A good solution is to wrapper your data access routine
into a class, making the connection object a global
variable that is opened in your class constructor.
Then write a dispose method for the class (which must
implement the IDisposable interface) which closes the
connection and destroys the object.
Make sure you call the dispose method of your class after
you have finished with it - the asp.net garbage collector
is unreliable.

alex
 
Back
Top