When the connection to the database will be destroyed?

  • Thread starter Thread starter Quentin Huo
  • Start date Start date
Q

Quentin Huo

Hi,

I want to create a class (named "classA") in which a connection to a
database will be built. In another class (named "classB"), an object of the
"classA" is created and data are retrieved from the connection of the object
of the "classA". But in C#, we never need explicitly destroy an object, so
when will the connection to the database in the object be disconnected? I am
using C# to write the ASP.NET pages and I don't want to keep the database
connection after data have been retrieved. Do I need to destroy the object
explicitly? If yes, how?

Thanks

Q.
 
You should close the connection as soon as you are done with it. You don't
need to destroy anything.
 
The Connection will be disconnected when you either Close or Dispose the
Connection object.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
On the connection object, you need to call Close() or Dispose(). (Close is
prefered.) On Class A, you should implement IDisposable and call the
connection's Close method in the Dispose method of IDisposable. ClassB
should call classA.Dispose() when done using the classA object.

Search the help for Dispose to get more info.
 
The Connection will be disconnected when you either Close or Dispose the
Connection object.

Not true under default options. Calling Close() or Dispose() will release
the connection back into the pool, and it will be closed some time later if
it goes unused.

Mark
 
So, there is not any way to disconnect the connection at once after
retrieving the date? In another words, it is not neccessary to disconnect
the connection in ASP.NET?

Thanks

Q.
 
If you do what I told you, everything will be fine. .Net manages the
Connection Pool.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top