473,725 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closing DataReader ? : Releasing Memory

I have a function which return datareader

Public Shared Function ReturnDReader(B yVal query As String) As
OleDbDataReader
Dim Connection_Stri ng As String =
System.Configur ation.Configura tionSettings.Ap pSettings("strC onn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection (Connection_Str ing)
cm = New OleDbCommand(qu ery, conn)

conn.Open()

dr = cm.ExecuteReade r(CommandBehavi or.CloseConnect ion)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Me ssage()

Finally

cm.Dispose()

End Try

End Function

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

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

DataList1.DataS ource = d

DataList1.DataB ind()

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 4128
Close the connection:

conn.Close();

Eliyahu

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

Public Shared Function ReturnDReader(B yVal query As String) As
OleDbDataReader
Dim Connection_Stri ng As String =
System.Configur ation.Configura tionSettings.Ap pSettings("strC onn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection (Connection_Str ing)
cm = New OleDbCommand(qu ery, conn)

conn.Open()

dr = cm.ExecuteReade r(CommandBehavi or.CloseConnect ion)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Me ssage()

Finally

cm.Dispose()

End Try

End Function

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

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

DataList1.DataS ource = d

DataList1.DataB ind()

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******** ******@tk2msftn gp13.phx.gbl...
I have a function which return datareader

Public Shared Function ReturnDReader(B yVal query As String) As
OleDbDataReader
Dim Connection_Stri ng As String =
System.Configur ation.Configura tionSettings.Ap pSettings("strC onn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection (Connection_Str ing)
cm = New OleDbCommand(qu ery, conn)

conn.Open()

dr = cm.ExecuteReade r(CommandBehavi or.CloseConnect ion)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Me ssage()

Finally

cm.Dispose()

End Try

End Function

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

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

DataList1.DataS ource = d

DataList1.DataB ind()

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.co m> wrote in message
news:eE******** ******@TK2MSFTN GP09.phx.gbl...
Close the connection:

conn.Close();

Eliyahu

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

Public Shared Function ReturnDReader(B yVal query As String) As
OleDbDataReader
Dim Connection_Stri ng As String =
System.Configur ation.Configura tionSettings.Ap pSettings("strC onn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection (Connection_Str ing)
cm = New OleDbCommand(qu ery, conn)

conn.Open()

dr = cm.ExecuteReade r(CommandBehavi or.CloseConnect ion)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Me ssage()

Finally

cm.Dispose()

End Try

End Function

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

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

DataList1.DataS ource = d

DataList1.DataB ind()

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.nu ll> wrote in message
news:uk******** ******@TK2MSFTN GP09.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******** ******@tk2msftn gp13.phx.gbl...
I have a function which return datareader

Public Shared Function ReturnDReader(B yVal query As String) As
OleDbDataReader
Dim Connection_Stri ng As String =
System.Configur ation.Configura tionSettings.Ap pSettings("strC onn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection (Connection_Str ing)
cm = New OleDbCommand(qu ery, conn)

conn.Open()

dr = cm.ExecuteReade r(CommandBehavi or.CloseConnect ion)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Me ssage()

Finally

cm.Dispose()

End Try

End Function

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

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

DataList1.DataS ource = d

DataList1.DataB ind()

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******** ******@TK2MSFTN GP09.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.co m> wrote in message
news:eE******** ******@TK2MSFTN GP09.phx.gbl...
Close the connection:

conn.Close();

Eliyahu

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

Public Shared Function ReturnDReader(B yVal query As String) As
OleDbDataReader
Dim Connection_Stri ng As String =
System.Configur ation.Configura tionSettings.Ap pSettings("strC onn")
Dim conn As OleDbConnection
Dim cm As OleDbCommand
Dim dr As OleDbDataReader
Try

conn = New OleDbConnection (Connection_Str ing)
cm = New OleDbCommand(qu ery, conn)

conn.Open()

dr = cm.ExecuteReade r(CommandBehavi or.CloseConnect ion)

Return dr

Catch ExceptRaise As OleDbException

'ExceptRaise.Me ssage()

Finally

cm.Dispose()

End Try

End Function

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

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

DataList1.DataS ource = d

DataList1.DataB ind()

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.co m> wrote in message
news:OW******** ******@TK2MSFTN GP09.phx.gbl...
Take connection object out of that function scope.

Eliyahu

"Arsalan" <ar***********@ hotmail.com> wrote in message
news:eF******** ******@TK2MSFTN GP09.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.co m> wrote in message
news:eE******** ******@TK2MSFTN GP09.phx.gbl...
> Close the connection:
>
> conn.Close();
>
> Eliyahu
>
> "Arsalan" <ar***********@ hotmail.com> wrote in message
> news:eB******** ******@tk2msftn gp13.phx.gbl...
>> I have a function which return datareader
>>
>> Public Shared Function ReturnDReader(B yVal query As String) As
>> OleDbDataReader
>> Dim Connection_Stri ng As String =
>> System.Configur ation.Configura tionSettings.Ap pSettings("strC onn")
>> Dim conn As OleDbConnection
>> Dim cm As OleDbCommand
>> Dim dr As OleDbDataReader
>> Try
>>
>> conn = New OleDbConnection (Connection_Str ing)
>> cm = New OleDbCommand(qu ery, conn)
>>
>> conn.Open()
>>
>> dr = cm.ExecuteReade r(CommandBehavi or.CloseConnect ion)
>>
>> Return dr
>>
>> Catch ExceptRaise As OleDbException
>>
>> 'ExceptRaise.Me ssage()
>>
>> Finally
>>
>> cm.Dispose()
>>
>> End Try
>>
>> End Function
>>
>> I call this function like this from Page_Load method
>> Dim d As Data.OleDb.OleD bDataReader
>>
>> d = CodeMaster.Func .ReturnDReader( "select * from employess")
>>
>> DataList1.DataS ource = d
>>
>> DataList1.DataB ind()
>>
>> 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
3045
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 Unix/Windows. Why use Python? End user flexibility. So far it seems that (on Windows): (1) memory increases when you import <module>. Expected behaviour. (2) memory does not release when you del <module>. Why not? Is Python
6
363
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 in ADO.NET in MSDN and there is a confusion regarding the buffering of data in case of DATAREADER. The link for MSDN JAN 2004 is -
6
750
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 object 1. DataReader dr = cmd.executereader(); 2.while(dr.read()) { // do something //} I have a break point in line 2. when i run my application and once it hits the break point (i.e line 2) I opened query analyzer and deleted all the rows in...
13
1794
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 technologies. I use codebehinds as standard Does anyone else find that developing with ASP.net takes more work than asp.
3
1831
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 SqlConnection(ConfigurationSettings.AppSettings("ConnectionString")) Dim ProductsByCategory As SqlDataReader = SqlHelper.ExecuteReader(myConnection, CommandType.StoredProcedure, "ProductsByCategory", New SqlParameter("@CategoryID", CategoryID))
7
1801
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
2783
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 wondering if I close the DataReader do I automatic ally close the Connection? If not , how can I close the Connection outside the ExecuteReader function?
7
3801
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 the changed? I'm using VS2005 with .net 3.0.
3
8536
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 following questions: 1. Can DataReader be bound DIRECTLY to DataGridView? 2. If DataReader cannot be bound directly to DataGridView, how can I load a DataReader into a DataTable and bind the DataTable to DataGridView? Can the DataTable be a...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.