.NET MySql 确实“使用”关闭数据读取器?

发布于 2024-09-16 23:21:36 字数 546 浏览 2 评论 0原文

我曾经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2024-09-23 23:21:36

该对象将被处置。是的,这将关闭 DataReader。

The object will be disposed. Yes, this closes the DataReader.

若言繁花未落 2024-09-23 23:21:36

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文