469,883 Members | 1,232 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,883 developers. It's quick & easy.

Closing DataReader ? : Releasing Memory

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
7 3962
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
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
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
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
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

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
> 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.

Similar topics

4 posts views Thread by lebo | last post: by
6 posts views Thread by Yasutaka Ito | last post: by
6 posts views Thread by Ravi | last post: by
13 posts views Thread by Simon Harvey | last post: by
3 posts views Thread by Paolo Pignatelli | last post: by
7 posts views Thread by Tumurbaatar S. | last post: by
3 posts views Thread by rockdale | last post: by
7 posts views Thread by =?Utf-8?B?UGV0ZXI=?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.