什么被处置时SqlCommand.Dispose叫? [英] What gets disposed when SqlCommand.Dispose is called?

查看:494
本文介绍了什么被处置时SqlCommand.Dispose叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在理论上以来的SqlCommand实现IDisposable SqlCommand对象应始终处置。我个人包装using语句周围。但是我看到很多code,从来没有部署的SqlCommand对象没有任何明显的问题。

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.

据我所知,终结最终会由垃圾收集,但自认为所谓的可以在大多数情况下,相当长一段时间(和从来没有在其他人),为什么不是code崩溃,由于耗尽了一些资源?

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?

在我们自己的code的基础,我们有全天候运行,不设置命令code。我希望把它清理干净,但它很难它不会造成任何问题,当自圆其说。

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.

推荐答案

在我看来就像它调用基类(组件)Dispose方法,和跳开始垃圾回收......

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

我用反射

这篇关于什么被处置时SqlCommand.Dispose叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆