PDA

View Full Version : Unable to close database connection after returning datareader object from function


barnardj
09-12-2003, 08:52 AM
I have created a class to handle database opening, updates, retrieval etc and with the retreival function
I return a sqldatareader object to the calling object. This shown below:

Public Function getRecords(ByRef sql As String) As SqlDataReader
'** Get a collection of records
Dim dr As SqlDataReader
Try
opendb()
Dim cmd As New SqlCommand(sql, objConn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return dr
Catch ex As Exception
WriteToEventLog(ex.Message)
closedb()
Return Nothing
End Try
End Function

However I am unable to close the database connection as this will effectively close the datareader and so the calling object
and ASP will not be able to display the contents. This means that I am left with a 'hanging' database connection and so I have had errors
with the connection pool reaching its max.

Is there any way I can close the database connection in this function without affecting the datareader object?

Thanks in advance for any help

Judith

DNAunion2000
09-12-2003, 09:12 PM
/*DNAunion*/ Why not just go with disconnected access? (That seems to be what you are trying to do anyway) Can't you use a dataset instead of a datareader?

barnardj
09-14-2003, 10:02 AM
Yes, disconnected mode is the way to go.
Thanks