use of MySQLConnection.dispose and/or MySQLCommand.dispose

A

Antonio Concepcion

Hi!
After you close the SQL Connection using

MySQLConnection.Close

Do you still need to do a dispose as in:

MySQLConnection.Dispose
and also
MySQLCommand.Dispose?

Is this good practice?

Thanks
Antonio Concepcion
 
T

Tod Birdsall

Hi Antonio,

If you view the SqlConnection Dispose() method using a disassembler (I
use .NET Reflector) you will see that all it does is check to see if
the ConnectionState is open and if it is, it calls the Close() method.

So, to answer your question, if you call Close() yourself, there is no
need to call the Dispose() method.

Tod Birdsall, MCP
http://tod1d.blogspot.com
 
A

Alvin Bruney [MVP]

Well, there really is more to it than that. By default, the connection
pooling is on. The default behavior of close flags the connection object as
reusable and returns it to the connection pool. After 60 seconds (default
timeout), the connection is truly closed. A closed connection may then have
the managed memory reclaimed. So either call has the same effect underneath
as rightly pointed out.

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
 
A

Antonio Concepcion

Thank you both!
So executing CLOSE on an SQLConnection is
sufficient. No further need for a DISPOSE.
Antonio

Alvin Bruney said:
Well, there really is more to it than that. By default, the connection
pooling is on. The default behavior of close flags the connection object
as reusable and returns it to the connection pool. After 60 seconds
(default timeout), the connection is truly closed. A closed connection may
then have the managed memory reclaimed. So either call has the same effect
underneath as rightly pointed out.

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Tod Birdsall said:
Hi Antonio,

If you view the SqlConnection Dispose() method using a disassembler (I
use .NET Reflector) you will see that all it does is check to see if
the ConnectionState is open and if it is, it calls the Close() method.

So, to answer your question, if you call Close() yourself, there is no
need to call the Dispose() method.

Tod Birdsall, MCP
http://tod1d.blogspot.com
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top