2010-08-27 54 views
2

我以前使用的try/catch关闭打开的DataReader/finally块:.NET MySql没有“使用”关闭数据读取器?

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 

但我认为它应该是清洁剂(和稍快)使用“使用” VB关键字:

Using dr As MySqlDataReader = DBConnection.callReadingStoredProcedure("my_sp") 

End Using 
' dr is surely disposed, but is it closed? 

IDispose接口(由Using使用)是否在DataReader上执行Close?

回答

7

该对象将被丢弃。是的,这会关闭DataReader。

+0

我相信它是继承IDataReader继承IDisposable。我相当肯定这是微软将执行的行为。 – IAbstract 2011-05-19 23:52:58

0

读取器将被关闭,但这不是必需的,因为它是使用ADO.NET连接池进行管理的,因此不需要进行数据库连接。 查看此答案的更多信息:C# MySqlConnection won't close

相关问题