473,471 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

connection close problem

sam
Hello group,
I have a function which is used to initiate sqlDataReader
object.
I was trying to invoke the close method on the DataReader object but
cant really do that as the function returns a datareader and cannot
access the datareader once the connection is closed.

Here is what I do:

public function getDataReader() as datareader
oCmd.Connection.Open() ( oCmd - command object)
Dim oDr As SqlDataReader =
oCmd.ExecuteReader(CommandBehavior.CloseConnection )
' cant add a odr.close()
Return oDr
End Function

How do I close a datareader explicitly and access it outside the
function.

Thanks,
Chris.
Nov 22 '05 #1
3 2516
Hi Sam:
"sam" <sr*****@mailcity.com> wrote in message
news:5c**************************@posting.google.c om...
Hello group,
I have a function which is used to initiate sqlDataReader
object.
I was trying to invoke the close method on the DataReader object but
cant really do that as the function returns a datareader and cannot
access the datareader once the connection is closed.

Here is what I do:

public function getDataReader() as datareader
oCmd.Connection.Open() ( oCmd - command object)
Dim oDr As SqlDataReader =
oCmd.ExecuteReader(CommandBehavior.CloseConnection )
' cant add a odr.close()
Return oDr
End Function
Returning datareaders is generally something you probably want to avoid. A
Reader is a 'connected' object and as such, is useless without a persistent
connection. As such, what you are asking to do , if I understand you
correctly, is imposssible. Remember also that ExecuteReader simply fires
the query but the data transfer hasn't happened yet. So if you close the
connection an/or the reader, that's pretty much it as far as that goes.

Also, Kathleen Dollard http://www.gendotnet.com/blog/ has really come out
against, for instance, passing data readers between layers. IN this case
you aren't exactly doing what she warns against but it's similar enough.
You'll notice that MS left out a method to return a reader from the Data
Access Application block.. and there's a good reason for it. To get
anything like this to work, you'd need to pass around a connection and a
reader and the whole thing is going to be kind of klunky (at best).

I'd recommend building a business object that mirrors the fields in the
Query the Reader is based on . Then build a collection of these objects
(strongly typed would be best) and fill the collection with the reader.
Then you can close the reader and the connection and just return the
Collection. This will give you all the functionality you need and provide a
much more safe and clean framework to work in.

HTH,

Bill
How do I close a datareader explicitly and access it outside the
function.

Thanks,
Chris.

W.G. Ryan, eMVP
http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
Nov 22 '05 #2
That's right.

You don't close the connection - keep the connection open and return the
datareader.

When you call Close on the datareader, that will automatically close
connection, since you are using CommandBehavior.CloseConnection.

If you don't close the reader, the connection will remain open until the GC
cleans it up, which may be a while, and more then likely you will run out of
connections in your connection pool.

"sam" <sr*****@mailcity.com> wrote in message
news:5c**************************@posting.google.c om...
Hello group,
I have a function which is used to initiate sqlDataReader
object.
I was trying to invoke the close method on the DataReader object but
cant really do that as the function returns a datareader and cannot
access the datareader once the connection is closed.

Here is what I do:

public function getDataReader() as datareader
oCmd.Connection.Open() ( oCmd - command object)
Dim oDr As SqlDataReader =
oCmd.ExecuteReader(CommandBehavior.CloseConnection )
' cant add a odr.close()
Return oDr
End Function

How do I close a datareader explicitly and access it outside the
function.

Thanks,
Chris.

Nov 22 '05 #3
yah.. Thats exactly whats happening. But coz I use this function in a loop
( pre-existing code), as a result the connection pool is running out of
connections.. So, I had found a hack which works fine as of now..I return
the datareader to the caller function where a datareader object is created.
Invoking a close on that datareader solves the problem :)
Ryan's method could be used but its too late for that..

Thanks for the reply..

"Marina" <so*****@nospam.com> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
That's right.

You don't close the connection - keep the connection open and return the
datareader.

When you call Close on the datareader, that will automatically close
connection, since you are using CommandBehavior.CloseConnection.

If you don't close the reader, the connection will remain open until the GC cleans it up, which may be a while, and more then likely you will run out of connections in your connection pool.

"sam" <sr*****@mailcity.com> wrote in message
news:5c**************************@posting.google.c om...
Hello group,
I have a function which is used to initiate sqlDataReader
object.
I was trying to invoke the close method on the DataReader object but
cant really do that as the function returns a datareader and cannot
access the datareader once the connection is closed.

Here is what I do:

public function getDataReader() as datareader
oCmd.Connection.Open() ( oCmd - command object)
Dim oDr As SqlDataReader =
oCmd.ExecuteReader(CommandBehavior.CloseConnection )
' cant add a odr.close()
Return oDr
End Function

How do I close a datareader explicitly and access it outside the
function.

Thanks,
Chris.


Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob | last post by:
I have an ASP.NET web application that has been running without any problems for a while. I recently transferred the site to shared hosting and had multiple users start to use the site. The problem...
3
by: sam | last post by:
Hello group, I have a function which is used to initiate sqlDataReader object. I was trying to invoke the close method on the DataReader object but cant really do that as the function returns a...
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?
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
8
by: Greg Strong | last post by:
Hello All, The short questions are 1 Do you know how to make DSN connection close in Access to Oracle 10g Express Edition? &/or 2 Do you know how to make a DSN-less pass-through query...
16
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.