英文:
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
语句,就不需要添加Close
或Dispose
。文档引用:
> 使用块的行为类似于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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论