2010-08-27 50 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

相關問題