When the connection to the database will be destroyed?

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.
 
M

Marina

You should close the connection as soon as you are done with it. You don't
need to destroy anything.
 
K

Kevin Spencer

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.
 
S

Scott English

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.
 
M

Mark

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
 
Q

Quentin Huo

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.
 
K

Kevin Spencer

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.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top