By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

Closing DataReader ? : Releasing Memory

P: n/a
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSett ings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()
But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the Page_Load
method then everything works fine, but problem occurs when this function is
in external class. the database doesnt close]
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Close the connection:

conn.Close();

Eliyahu

"Arsalan" <ar***********@hotmail.com> wrote in message
news:eB**************@tk2msftngp13.phx.gbl...
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSett ings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()
But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the Page_Load
method then everything works fine, but problem occurs when this function is in external class. the database doesnt close]

Nov 19 '05 #2

P: n/a
Hi Arsalan.
I think it's a bad idea to return DataReader.
That is because the DataReader holds the connection open,
and you depend on the caller to close it.
I suggest you make a function that returns a DataView.
Sharon.

"Arsalan" <ar***********@hotmail.com> wrote in message
news:eB**************@tk2msftngp13.phx.gbl...
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSett ings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()
But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the Page_Load
method then everything works fine, but problem occurs when this function is in external class. the database doesnt close]

Nov 19 '05 #3

P: n/a
Good idea buddy, but you cannot close the connection and read data from
DataReader [please see the code], and as connection object is out of scope,
you cannot close it through Page_Load event.
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
Close the connection:

conn.Close();

Eliyahu

"Arsalan" <ar***********@hotmail.com> wrote in message
news:eB**************@tk2msftngp13.phx.gbl...
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSett ings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()
But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the
Page_Load
method then everything works fine, but problem occurs when this function

is
in external class. the database doesnt close]


Nov 19 '05 #4

P: n/a
Thanks,
"Sharon" <sh****@void.null> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...
Hi Arsalan.
I think it's a bad idea to return DataReader.
That is because the DataReader holds the connection open,
and you depend on the caller to close it.
I suggest you make a function that returns a DataView.
Sharon.

"Arsalan" <ar***********@hotmail.com> wrote in message
news:eB**************@tk2msftngp13.phx.gbl...
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSett ings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()
But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the
Page_Load
method then everything works fine, but problem occurs when this function

is
in external class. the database doesnt close]


Nov 19 '05 #5

P: n/a
Take connection object out of that function scope.

Eliyahu

"Arsalan" <ar***********@hotmail.com> wrote in message
news:eF**************@TK2MSFTNGP09.phx.gbl...
Good idea buddy, but you cannot close the connection and read data from
DataReader [please see the code], and as connection object is out of scope, you cannot close it through Page_Load event.
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
Close the connection:

conn.Close();

Eliyahu

"Arsalan" <ar***********@hotmail.com> wrote in message
news:eB**************@tk2msftngp13.phx.gbl...
I have a function which return datareader

Public Shared Function ReturnDReader(ByVal query As String) As
OleDbDataReader
Dim Connection_String As String =
System.Configuration.ConfigurationSettings.AppSett ings("strConn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection(Connection_String)
cm = New OleDbCommand(query, conn)

conn.Open()

dr = cm.ExecuteReader(CommandBehavior.CloseConnection)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Message()

Finally

cm.Dispose()

End Try

End Function

I call this function like this from Page_Load method
Dim d As Data.OleDb.OleDbDataReader

d = CodeMaster.Func.ReturnDReader("select * from employess")

DataList1.DataSource = d

DataList1.DataBind()

d.Close()
But for some reason after closing the datareader, the database is not
closing. (I'm using Access and record locking file is still there)

How do I resolve this ? [If the function's code is copied to the
Page_Load
method then everything works fine, but problem occurs when this
function is
in external class. the database doesnt close]



Nov 19 '05 #6

P: n/a

Good suggestion but coding everything would be tedious, is there any
alternate way to do it ??
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:OW**************@TK2MSFTNGP09.phx.gbl...
Take connection object out of that function scope.

Eliyahu

"Arsalan" <ar***********@hotmail.com> wrote in message
news:eF**************@TK2MSFTNGP09.phx.gbl...
Good idea buddy, but you cannot close the connection and read data from
DataReader [please see the code], and as connection object is out of

scope,
you cannot close it through Page_Load event.
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:eE**************@TK2MSFTNGP09.phx.gbl...
> Close the connection:
>
> conn.Close();
>
> Eliyahu
>
> "Arsalan" <ar***********@hotmail.com> wrote in message
> news:eB**************@tk2msftngp13.phx.gbl...
>> I have a function which return datareader
>>
>> Public Shared Function ReturnDReader(ByVal query As String) As
>> OleDbDataReader
>> Dim Connection_String As String =
>> System.Configuration.ConfigurationSettings.AppSett ings("strConn")
>> Dim conn As OleDbConnection
>> Dim cm As OleDbCommand
>> Dim dr As OleDbDataReader
>> Try
>>
>> conn = New OleDbConnection(Connection_String)
>> cm = New OleDbCommand(query, conn)
>>
>> conn.Open()
>>
>> dr = cm.ExecuteReader(CommandBehavior.CloseConnection)
>>
>> Return dr
>>
>> Catch ExceptRaise As OleDbException
>>
>> 'ExceptRaise.Message()
>>
>> Finally
>>
>> cm.Dispose()
>>
>> End Try
>>
>> End Function
>>
>> I call this function like this from Page_Load method
>> Dim d As Data.OleDb.OleDbDataReader
>>
>> d = CodeMaster.Func.ReturnDReader("select * from employess")
>>
>> DataList1.DataSource = d
>>
>> DataList1.DataBind()
>>
>> d.Close()
>>
>>
>> But for some reason after closing the datareader, the database is not
>> closing. (I'm using Access and record locking file is still there)
>>
>> How do I resolve this ? [If the function's code is copied to the
>> Page_Load
>> method then everything works fine, but problem occurs when this function > is
>> in external class. the database doesnt close]
>>
>>
>
>



Nov 19 '05 #7

P: n/a
> I think it's a bad idea to return DataReader.
That is because the DataReader holds the connection open,


That depends upon his application really. Nothing wrong as such as long as,
as you say, it's closed correctly.

DataReader's are supposed to be fast but for small data sets, I've not found
much difference between a DataReader and a DataSet,

Rob.
Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.