SqlCommand dispose and it's connection property

  • Thread starter Thread starter Big D
  • Start date Start date
B

Big D

If you define a sqlCommands connection property directly as in:

mySqlCommand.Connection = new sqlConnection(connectionString)

When you call dispose on the command, does it automatically call the dispose
method of the connection? It seems like this is the reason to have a
dispose property (iterates through the child components and disposes of them
if appropriate??), but it's new to me, so I'm not sure.

Thanks!

MCD
 
no, its only effect on the connection is that it dereferences the
connection. if it was the last reference, then connection will only be
closed when the GC decides collect it.


-- bruce (sqlwork.com)
 
Thanks for Bruce's informative suggestions.

Hi MCD,

As Bruce has mentioned, the Dispose method of the sqlCommand won't cause
its connection also dispose and only make the connection unreferenced by
itself.
#SqlCommand.IDisposable.Dispose Method
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataSqlClientS
qlCommandClassIDisposableDisposeTopic.asp?frame=true

Also, as for the Dispose method, it's a member function in the IDisposable
Interface, and most dotnet framework buildin classes have implemented this
interface which helped to control the management of managed and unmanaged
resource the certain class handles. And here are some related tech
reference in MSDN:

#IDisposable Interface
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemIDisposableCla
ssTopic.asp?frame=true

#Implementing a Dispose Method
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconimplementingdispos
emethod.asp?frame=true

Hope these helpful.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Hi MCD,

Have you had a chance to check the suggestions in my last reply or have you
got any ideas on this questions? If you have anything unclear or have any
other questions, please feel free to post here. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top