DataReader question

G

Guest

Is the following code a valid way of passing a DataReader object back to
calling code? I'm specifically concerned with the objConn object be
explicitly set to Nothing. Does this cause the connection object to be
garbage collected or does the connection object not get destroyed until the
calling code closes the DataReader object (and thus the connection object)?

Public Function GetDataReader(ByVal strSelect As String) As
OleDbDataReader
Try
objConn = New OleDbConnection(strConnectionString)
objCmd = New OleDbCommand(strSelect, objConn)
objCmd.Connection.Open()
GetDataReader =
objCmd.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As OleDbException
GetDataReader = Nothing
Finally
objConn = Nothing
objCmd = Nothing
End Try
End Function
 
W

William \(Bill\) Vaughn

Since you coded "CommandBehavior.CloseConnection), the client should (must)
close the DataReader when it's done with it. This will close the Connection
(return it to the pool) and mark its resources for cleanup on next GC.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top