我曾经使用 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
但我认为使用“使用”VB 关键字应该更干净(并且更快):
Using dr As MySqlDataReader = DBConnection.callReadingStoredProcedure("my_sp")
End Using
' dr is surely disposed, but is it closed?
IDispose 接口(Using 需要)是否在 DataReader 上执行关闭?