.NET MySql 确实“使用”关闭数据读取器?
我曾经使用 try/catch/finally 块关闭打开的数据读取器:
Dim dr As MySqlDataReader = Nothing
Try
dr = DBConnection.callReadingStoredProcedure("my_sp")
Catch ex As Exception
' the caller will handle this
Throw ex
Finally
If dr IsNot Nothing Then dr.Close()
End Try
但我认为使用“Using”VB 关键字应该更干净(并且更快):
Using dr As MySqlDataReader = DBConnection.callReadingStoredProcedure("my_sp")
End Using
' dr is surely disposed, but is it closed?
IDispose 接口(Using 需要)是否执行 Close on数据读取器?
I used to close an open datareader using the try/catch/finally block:
Dim dr As MySqlDataReader = Nothing
Try
dr = DBConnection.callReadingStoredProcedure("my_sp")
Catch ex As Exception
' the caller will handle this
Throw ex
Finally
If dr IsNot Nothing Then dr.Close()
End Try
But I think it should be cleaner (and somewhat faster) to use the "Using" VB keyword:
Using dr As MySqlDataReader = DBConnection.callReadingStoredProcedure("my_sp")
End Using
' dr is surely disposed, but is it closed?
Does the IDispose interface (required by Using) perform a Close on the DataReader?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该对象将被处置。是的,这将关闭 DataReader。
The object will be disposed. Yes, this closes the DataReader.
Reader 将关闭,但这对于底层数据库连接来说不是必需的,因为它是通过 ADO.NET 连接池进行管理的。
检查此答案以获取更多信息:C# MySqlConnection won't close
Reader will be closed, but this is not necessary for underlaying database connection because it is managed with ADO.NET connection pool.
Check this answer for more information: C# MySqlConnection won't close