What gets disposed when SqlCommand.Dispose is called?

人走茶凉 提交于 2020-01-04 02:28:42

问题


In theory since SqlCommand implements IDisposable a SqlCommand object should always be disposed. I personally wrap a using statement around them. However I see lots of code that never disposes of SqlCommand objects without any apparent problems.

I understand that finalizers will ultimately be called by garbage collection but since that can take quite a while in most cases (and never in others) why isn't the code crashing due to running out of some resource?

In our own code base we have code that runs 24x7 without disposing of commands. I'd like to clean it up but it's hard to justify when it's not causing any problems.


回答1:


Looks to me like it calls the base class (Component) Dispose method, and jump-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 by using Reflector.



来源:https://stackoverflow.com/questions/1828788/what-gets-disposed-when-sqlcommand-dispose-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!