What happens when SqlCommand.Dispose is called?

In theory, since SqlCommand implements IDisposable, the SqlCommand object should always be deleted. I personally wrap the expression of use around myself. However, I see a lot of code that never disposes of SqlCommand objects without any obvious problems.

I understand that finalizers will ultimately be caused by garbage collection, but since in most cases this can take quite a while (and never in others), why doesn’t the code crash due to the expiration of some resource?

In our own code base, we have code that runs 24x7 without utilization of commands. I would like to clean it, but it is hard to justify when it does not cause any problems.

+3
sql-server
source share
1 answer

It seems to me that it calls the Dispose base class (Component) method and starts garbage collection ...

Public Sub Dispose(ByVal disposing As Boolean) If disposing Then 'Same as Component.Dispose() SyncLock Me If Me.site IsNot Nothing AndAlso Me.site.Container IsNot Nothing Then Me.site.Container.Remove(Me) End If If Me.events IsNot Nothing Then Dim handler As EventHandler = DirectCast(Me.events.Item(Component.EventDisposed), EventHandler) If handler IsNot Nothing Then handler.Invoke(Me, EventArgs.Empty) End If End If End SyncLock End If GC.SuppressFinalize(Me) End Sub 

I got this using a Reflector .

+2
source share

All Articles