需要Dispose吗?

huangapple go评论72阅读模式
英文:

Do i need to Dispose?

问题

I have an application and some of the code is written like the example.
Is the Connection.Dispose needed? Would it be causing any problems?
Thanks!

使用 Connection 时,需要 Connection.Dispose 吗?它会引起任何问题吗?
谢谢!

UPDATED:

更新后的代码如下:

Using Connection As New SqlConnection(ConfigurationManager.ConnectionStrings("DBCONSTRING").ConnectionString)
    Dim command As New SqlCommand("Select TextData from Table1 where key = 1", Connection)
    Connection.Open()
    Using reader As SqlDataReader = command.ExecuteReader
        While reader.Read()
            Dim T1 As String = reader("TextData").ToString()
        End While
    End Using
End Using
英文:

I have an application and some of the code is written like the example.
Is the Connection.Dispose needed? Would it be causing any problems?
Thanks!

        Using Connection As New SqlConnection(ConfigurationManager.ConnectionStrings("DBCONSTRING").ConnectionString)
            Dim command As New SqlCommand("Select TextData from Table1 where key = 1", Connection)
            Connection.Open()
            Dim reader As SqlDataReader = command.ExecuteReader()
            Try
                While reader.Read()

                    Dim T1 As String = reader("TextData").ToString()

                End While
            Finally
                reader.Close()
            End Try
            Connection.Close()
            Connection.Dispose()
        End Using

UPDATED:

        Using Connection As New SqlConnection(ConfigurationManager.ConnectionStrings("DBCONSTRING").ConnectionString)
            Dim command As New SqlCommand("Select TextData from Table1 where key = 1", Connection)
            Connection.Open()
            Using reader As SqlDataReader = command.ExecuteReader
                While reader.Read()
                    Dim T1 As String = reader("TextData").ToString()
                 End While
            End Using
        End Using

答案1

得分: 4

正如其他人所说:如果您使用Using语句,就不需要添加CloseDispose文档引用:

> 使用块的行为类似于Try...Finally结构,其中
> Try块使用资源,Finally块处置资源。
> 因此,使用块保证了资源的处置,
> 无论您如何退出块。即使在
> 未处理的异常情况下也是如此。

因此,您应该使用以下代码:

Dim T1 As String = Nothing
Using Connection As New SqlConnection(ConfigurationManager.ConnectionStrings("DBCONSTRING").ConnectionString)
    Using Command As New SqlCommand("Select TextData from Table1 where key = 1", Connection)
        Connection.Open()
        Using reader As SqlDataReader = Command.ExecuteReader
            While reader.Read()
                T1 = reader("TextData").ToString()
            End While
        End Using
    End Using
End Using

一般来说,对于实现IDisposable的所有内容都应该使用Using语句。

顺便说一下,由于您只选择一个值,您也可以使用ExecuteScalar

Using Connection As New SqlConnection(ConfigurationManager.ConnectionStrings("DBCONSTRING").ConnectionString)
    Using command As New SqlCommand("Select TOP 1 TextData from Table1 where key = 1", Connection)
        Connection.Open()
        Dim tmp As Object = command.ExecuteScalar()
        Dim T1 As String = If(Convert.IsDBNull(tmp), Nothing, DirectCast(tmp, String))
    End Using
End Using
英文:

As others said: no, you don't need to add Close or Dispose if you use the Using-statement. Documentation quote:

> A Using block behaves like a Try...Finally construction in which the
> Try block uses the resources and the Finally block disposes of them.
> Because of this, the Using block guarantees disposal of the resources,
> no matter how you exit the block. This is true even in the case of an
> unhandled exception

So you should use this:

Dim T1 As String = Nothing
Using Connection As New SqlConnection(ConfigurationManager.ConnectionStrings("DBCONSTRING").ConnectionString)
    Using Command As New SqlCommand("Select TextData from Table1 where key = 1", Connection)
        Connection.Open()
        Using reader As SqlDataReader = Command.ExecuteReader
            While reader.Read()
                T1 = reader("TextData").ToString()
            End While
        End Using
    End Using
End Using

In general: use the Using statement for everything that implements IDisposable.

By the way, since you only select a single value you could also use ExecuteScalar:

Using Connection As New SqlConnection(ConfigurationManager.ConnectionStrings("DBCONSTRING").ConnectionString)
    Using command As New SqlCommand("Select TOP 1 TextData from Table1 where key = 1", Connection)
        Connection.Open()
        Dim tmp As Object = command.ExecuteScalar()
        Dim T1 As String = If(Convert.IsDBNull(tmp), Nothing, DirectCast(tmp, String))
    End Using
End Using

huangapple
  • 本文由 发表于 2023年7月6日 17:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定