473,396 Members | 1,748 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,396 software developers and data experts.

closing DataReader in another layer

DS
Is there a way to automatically close the data reader connection? I'm using
the MS Data Access Application block to substantially {entirely} separate
the data access layer (DAL) from the business layer (BL) and this is great
with DataSets since they can be closed off in the DAL, but it doesn't seem
possible with the DataReader since it would need to be .close() in the BL
once I'm done with it.

I've read on the web that there is a bit of a debate about using something
like this:
using (DataReader dr = ExecuteReader(...))
{
return dr;
}
-- supposedly this automatically calls the dispose and closes the connection
once you're done with it in the BL, but some say its no good.

How should I go about making use of it, or should I just stick to DataSets?

Thanks in advance.
Nov 16 '05 #1
7 3996
Try this:

theReader = theConnection.ExecuteReader(CommandBehavior.CloseC onnection);
return theReader;

CommandBehaviour.CloseConnection makes the reader so that when the reader is
closed, the underlying connection gets closed too. This allows you to call
Close() on the reader somewhere else, and not leak a connection.

However, you might consider DataSets for a different reason: They actually
can go across physical layers, not just logical ones.

--
Michael Giagnocavo
MVP
www.atrevido.net
"DS" <no****************@rogers.com> wrote in message
news:_d********************@rogers.com...
Is there a way to automatically close the data reader connection? I'm
using the MS Data Access Application block to substantially {entirely}
separate the data access layer (DAL) from the business layer (BL) and this
is great with DataSets since they can be closed off in the DAL, but it
doesn't seem possible with the DataReader since it would need to be
.close() in the BL once I'm done with it.

I've read on the web that there is a bit of a debate about using something
like this:
using (DataReader dr = ExecuteReader(...))
{
return dr;
}
-- supposedly this automatically calls the dispose and closes the
connection once you're done with it in the BL, but some say its no good.

How should I go about making use of it, or should I just stick to
DataSets?

Thanks in advance.

Nov 16 '05 #2
Hi,

I think you should use the CommandBehavior Enumeration in the parameter to
Executereader method. Set it to CloseConnection so that when ur BL closed
the reader, the connection is automatically closed.

Regards
Joyjit

"DS" <no****************@rogers.com> wrote in message
news:_d********************@rogers.com...
Is there a way to automatically close the data reader connection? I'm using the MS Data Access Application block to substantially {entirely} separate
the data access layer (DAL) from the business layer (BL) and this is great
with DataSets since they can be closed off in the DAL, but it doesn't seem
possible with the DataReader since it would need to be .close() in the BL
once I'm done with it.

I've read on the web that there is a bit of a debate about using something
like this:
using (DataReader dr = ExecuteReader(...))
{
return dr;
}
-- supposedly this automatically calls the dispose and closes the connection once you're done with it in the BL, but some say its no good.

How should I go about making use of it, or should I just stick to DataSets?
Thanks in advance.

Nov 16 '05 #3
DS
"Michael Giagnocavo [MVP]" <mg*******@atrevido.net> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
Try this:

theReader = theConnection.ExecuteReader(CommandBehavior.CloseC onnection);
return theReader;

CommandBehaviour.CloseConnection makes the reader so that when the reader
is closed, the underlying connection gets closed too. This allows you to
call Close() on the reader somewhere else, and not leak a connection.

However, you might consider DataSets for a different reason: They actually
can go across physical layers, not just logical ones.

--
Michael Giagnocavo
MVP
www.atrevido.net


Thanks for the reply Michael,
I'd like to not even have to call the .Close on the reader in the business
tier as it is easy to forget to do, especially down the road. With a dataset
its not a big deal since I can close the connection in the DAL before the
dataset even gets returned and never have to worry about it in the buiness
layer. With a dataset if I forget to dispose it, at least that gets taken
care of by the garbage collection, whilst I believe a connection close does
not happen correctly on garbage collection of the DataReader.

Cheers,
Dan

Nov 16 '05 #4
> >
However, you might consider DataSets for a different reason: They actually can go across physical layers, not just logical ones.

I'd like to not even have to call the .Close on the reader in the business
tier as it is easy to forget to do, especially down the road. With a

dataset its not a big deal since I can close the connection in the DAL before the
dataset even gets returned and never have to worry about it in the buiness
layer.


Hi Dan,

If you have truly seperated the data layer from the business layer, then you
really should use the dataset. A lot of folks pan the dataset because the
data is read into an object that gets passed around and it takes time to
load up the object. The same folks then encapsulate their data into an
object and, guess what, they pass it around, rarely adding much more
functionality to it than the dataset already has.

One thing that I have done, in my code, is to create an object that contains
a dataset. I pass my object into my DAL, which fills the contained dataset
with data. I then pass my object around, which as some additional
functionality for validating and enforcing business rules. When it comes
time to update the database, I pass my object back to the DAL, which then
uses the internal dataset to update the database. That way, I get the
functionality of the Dataset, plus the business rules enforcement of my
application, with a minimum of code to debug.

(I don't believe in re-inventing the wheel).

Hope this helps,
--- Nick
Nov 16 '05 #5
Well, if you don't want to call Close, i.e., manage resources, then don't
use unmanaged resources (like a DB connection). A datareader is a directly
connected reader to get data out of the DB. If you find that it's hard to
keep track of in other layers and so on, then you should not be using it.

--
Michael Giagnocavo
MVP
www.atrevido.net
"DS" <no****************@rogers.com> wrote in message
news:Yf********************@rogers.com...
"Michael Giagnocavo [MVP]" <mg*******@atrevido.net> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
Try this:

theReader = theConnection.ExecuteReader(CommandBehavior.CloseC onnection);
return theReader;

CommandBehaviour.CloseConnection makes the reader so that when the reader
is closed, the underlying connection gets closed too. This allows you to
call Close() on the reader somewhere else, and not leak a connection.

However, you might consider DataSets for a different reason: They
actually can go across physical layers, not just logical ones.

--
Michael Giagnocavo
MVP
www.atrevido.net


Thanks for the reply Michael,
I'd like to not even have to call the .Close on the reader in the business
tier as it is easy to forget to do, especially down the road. With a
dataset its not a big deal since I can close the connection in the DAL
before the dataset even gets returned and never have to worry about it in
the buiness layer. With a dataset if I forget to dispose it, at least that
gets taken care of by the garbage collection, whilst I believe a
connection close does not happen correctly on garbage collection of the
DataReader.

Cheers,
Dan


Nov 16 '05 #6
DS

"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:akBpd.567042$mD.385239@attbi_s02...
>
> However, you might consider DataSets for a different reason: They actually > can go across physical layers, not just logical ones.
>


I'd like to not even have to call the .Close on the reader in the
business
tier as it is easy to forget to do, especially down the road. With a

dataset
its not a big deal since I can close the connection in the DAL before the
dataset even gets returned and never have to worry about it in the
buiness
layer.


Hi Dan,

If you have truly seperated the data layer from the business layer, then
you
really should use the dataset. A lot of folks pan the dataset because the
data is read into an object that gets passed around and it takes time to
load up the object. The same folks then encapsulate their data into an
object and, guess what, they pass it around, rarely adding much more
functionality to it than the dataset already has.

One thing that I have done, in my code, is to create an object that
contains
a dataset. I pass my object into my DAL, which fills the contained
dataset
with data. I then pass my object around, which as some additional
functionality for validating and enforcing business rules. When it comes
time to update the database, I pass my object back to the DAL, which then
uses the internal dataset to update the database. That way, I get the
functionality of the Dataset, plus the business rules enforcement of my
application, with a minimum of code to debug.

(I don't believe in re-inventing the wheel).

Hope this helps,
--- Nick


Hi Nick,
Thanks for the response, and yes it does help. I'm coming from an
ASP/VBScript/ADO background, and for scaleable website the best bet was to
always dump the recordset into an array, that way you could cut the
connection to the database right away (important for web development), and
you wouldn't have the overhead of having the ADO component floating around.
I know DataSets resolve both issues, but since in some cases I'm dealing
with such simple data I thought it may be better to just toss it into an
array and get it in there with the firehose (which I think is what the
DataReader uses anyway). In this case the data is just a single column of
primary key (int) numbers that can be dumped into an integer 1D array. What
do you think?

The second reason I'm doing it is because the result (be it a DataSet that
contains, just 1 table, and that table contains just 1 column, OR just a
simple array) is being returned from a static method (the method is used to
find/search for a given string and return all the recordIds that contain
it). So it seems from a program simplicity/design/maintainability/logic side
of things it makes sense to return the simple int array because it is fairly
self explanitory (just a list of Ids), rather than a DataSet which is a bit
more ambigious.

I'd appreciate any input you have with this approach since as I said, I'm
kind'a new to this :-)

Thanks again,
Dan
Nov 16 '05 #7
Hello!
If you have truly seperated the data layer from the business layer, then you really should use the dataset.
If speed is of high importance, you can't beat a datareader populating your
own custom models. The dataset is great, but don't forget that the joy of
filling a dataset comes with a performance degradation.
The same folks then encapsulate their data into an
object and, guess what, they pass it around, rarely adding much more
functionality to it than the dataset already has.


There's nothing wrong with that, if you can justify writing your own models.
I agree that there are many situations where you'd want to wrap / use typed
datarows (and the other objects from the dataset), and it's a timesaver when
combined with code generation.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #8

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

Similar topics

1
by: VIJAY KUMAR | last post by:
Hi, Problem: Return ArrayList where Values are Added using DataReader. Note: My Data Reader returns M Columns and N Rows I am using DataReader in Data Access layer. I want to return this...
6
by: Steven Blair | last post by:
Hi, I am writing an application using a 3 tier model (Client, Business Layer and DB) The DB layer creates a OdbcDataReader object back up to the Client where the data is read and displayed on...
4
by: VB Programmer | last post by:
I have a function that returns a datareader. The problem is that in the function's FINALLY I close the datareader and set it to nothing. But, BEFORE the Finally I "Return" the datareader (in the...
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: Arsalan | last post by:
I have a function which return datareader Public Shared Function ReturnDReader(ByVal query As String) As OleDbDataReader Dim Connection_String As String =...
4
by: hazz | last post by:
The data access layer below returns, well, a mess as you can see on the last line of this posting. What is the best way to return customer objects via a datareader from the data layer into my view...
11
by: ^MisterJingo^ | last post by:
Hi all, I have a form with 4 dropdownlist controls which I populate with data from DB tables. I have a class with a method which constructs a dataset, putting each DB table into a dataset table....
7
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.