473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 4104
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lebo | last post by:
Hi I'm trying to understand how Python handles memory usage and dynamic object loading and unloading. Problem to solve? Build a very low memory footprint (non-GUI) Python application for...
6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
6
by: Ravi | last post by:
Hi, I am not able to understand why a datareader needs a connection to the DB all the time. Here is what I tried. Sqlcommand cmd = ("select * from table1",con) // where con is the connection...
13
by: Simon Harvey | last post by:
Hi All, I have a colleague that I wprk with that develops using ASP. I develop using ASP.net. He seems to make sites much faster than me and I am wondering if its because of the two different...
3
by: Paolo Pignatelli | last post by:
I have a Function in a Class File that uses ApplicationBlocks: Public Function GetProductsByCategory(ByVal CategoryID As Integer) Dim myConnection As SqlConnection = New...
7
by: Tumurbaatar S. | last post by:
Is it so important to close database connections? As I understand, after processing a request and sending a response, page object destroyed. So all used connections also destroyed. Yes?
3
by: rockdale | last post by:
I am using Microsoft.Practices.Enterprise.Library When I use IDataReader I saw this line "It is the responsibility of the caller to close the connection and reader when finished." I was...
7
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I have read some articles state that DataSet should NOT be used for large resultset. What does "large" mean? Is "large" based on # of rows/columns and/or memory required to hold the original and...
3
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to add a datagridview control to a Windows Form to display read-only information in visual basic 2005. My understanding is that datareader will be faster for this purpose. I have the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.