Closing of OleDbConnection and OleDbDataReader

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
 
A

alex bowers

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top