2

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?

vulkanino
  • 8,858
  • 6
  • 38
  • 67

2 Answers2

7

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

Kevin Crowell
  • 9,476
  • 4
  • 32
  • 51
  • I believe it is inherited from IDataReader inheriting IDisposable. I'm fairly certain this is the behavior Microsoft would enforce. – IAbstract May 19 '11 at 23:52
0

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

Community
  • 1
  • 1
watbywbarif
  • 5,657
  • 7
  • 39
  • 56