【问题标题】:.NET MySql does "Using" closes the datareader?.NET MySql 是否“使用”关闭数据读取器?
【发布时间】:2010-08-27 17:02:13
【问题描述】:

我曾经使用 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 需要)是否在 DataReader 上执行关闭?

【问题讨论】:

    标签: mysql vb.net mysqldatareader


    【解决方案1】:

    对象将被释放。是的,这会关闭 DataReader。

    【讨论】:

    • 我相信它是继承自 IDataReader 继承 IDisposable。我相当肯定这是微软会强制执行的行为。
    【解决方案2】:

    Reader 将被关闭,但这对于底层数据库连接不是必需的,因为它是由 ADO.NET 连接池管理的。 查看此答案以获取更多信息:C# MySqlConnection won't close

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-16
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      相关资源
      最近更新 更多