473,657 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4008
Try this:

theReader = theConnection.E xecuteReader(Co mmandBehavior.C loseConnection) ;
return theReader;

CommandBehaviou r.CloseConnecti on 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******** ************@ro gers.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******** ************@ro gers.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*******@atre vido.net> wrote in message
news:O9******** ******@TK2MSFTN GP09.phx.gbl...
Try this:

theReader = theConnection.E xecuteReader(Co mmandBehavior.C loseConnection) ;
return theReader;

CommandBehaviou r.CloseConnecti on 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******** ************@ro gers.com...
"Michael Giagnocavo [MVP]" <mg*******@atre vido.net> wrote in message
news:O9******** ******@TK2MSFTN GP09.phx.gbl...
Try this:

theReader = theConnection.E xecuteReader(Co mmandBehavior.C loseConnection) ;
return theReader;

CommandBehaviou r.CloseConnecti on 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*******@hotm ail.nospam.com> wrote in message
news:akBpd.5670 42$mD.385239@at tbi_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
1397
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 result to Business Layer. But, considering performace and Efficiency, I have to Close the DataReader Connection.
6
1850
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 the screen. I then call the Close method of this OdbcDataReader. I had assumed this closed the connection to the Database, but apparently not (using processlist in MySQL confirmed the connections were not being closed). How can I successfully...
4
3711
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 Try). The problem is, by the time it's "returned" it says I already closed it. Any ideas how I can overcome this? How can I clean up the objects nicely WHILE returning it back to the caller???? Example:
13
1783
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
1828
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
4124
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 = System.Configuration.ConfigurationSettings.AppSettings("strConn") Dim conn As OleDbConnection Dim cm As OleDbCommand Dim dr As OleDbDataReader Try
4
3515
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 and bind them to a datagrid using BindingList. I am in need of an epiphany here. Thank you. -Greg ******************BIND ARRAY OF CUSTOMER OBJECTS TO DATAGRID ************************ private Customer c; c =...
11
1718
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. I then return the DS and bind the tables to the relevant controls. I've been reading that DataReaders are much more efficient than DS's for getting data from the database. So would it better to have 4 methods, each returning a dataReader, or...
7
2903
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 returns N rows. DataReader object? Collection object? DataTable object? Returning a DataReader object is not a good practice...right? Thnks for all your suggestions!!
0
8827
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
8732
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...
1
8504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
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
6169
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
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
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.