Proper database connection disposal methods??

R

Roger Twomey

Hi

I have been disposing of my sql connections for some time (as I should) but
when I see samples I noticed that the samples don't close the connection
first. Here is what I do:

sqlconnection.open

Code

sqlconnection.close
sqlconnection.dispose()

-->>
What I see in samples looks like this:

sqlconnection.open

code

sqlconnection.dispose()

-->>

I noticed that in all the samples (where I noticed at all) that no one was
closing the connection first. Should I bother? Is one method right and the
other wrong? Are there any advantages to closing first or should I NOT close
first?

Does anyone know the answer to this?

Thanks.
 
C

Chris R. Timmons

Hi

I have been disposing of my sql connections for some time (as I
should) but when I see samples I noticed that the samples don't
close the connection first. Here is what I do:

sqlconnection.open

Code

sqlconnection.close
sqlconnection.dispose()

-->>
What I see in samples looks like this:

sqlconnection.open

code

sqlconnection.dispose()

-->>

I noticed that in all the samples (where I noticed at all) that
no one was closing the connection first. Should I bother? Is one
method right and the other wrong? Are there any advantages to
closing first or should I NOT close first?

Does anyone know the answer to this?

Roger,

Dispose() calls Close() if the connection is still open. Use
Reflector (http://www.aisto.com/roeder/dotnet/) to examine how
SqlConnection.Dispose() operates if you're curious.
 
R

Roger Twomey

Thank you.

I suspected that it did.

Do you know if there is any advantage to my closing it explicitly? Do I save
any clock cycles or is it the same either way (except of course that I have
to type more).

Thanks again!
 
C

Chris R. Timmons

Thank you.

I suspected that it did.

Do you know if there is any advantage to my closing it
explicitly? Do I save any clock cycles or is it the same either
way (except of course that I have to type more).

Roger,

The performance is going to be essentially the same either way.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top