473,769 Members | 4,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataReader and DAL

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...righ t?

Thnks for all your suggestions!!
Nov 10 '06 #1
7 2907

Use the IDataReader object.
I prefer to ~consume IDataReader's in the Biz Layer, but to NOT send them
"up" to the Presentation layer.

See
http://sholliday.spaces.live.com/blog/
5/24/2006
Custom Objects/Collections and Tiered Development
for a complete downloadable example.

Also .. at the bottom of my blog entry, there is a MS article (which I say
"read top to bottom" or something like that)
It will give you a good birds eye view.
"Diffident" <Di*******@disc ussions.microso ft.comwrote in message
news:FB******** *************** ***********@mic rosoft.com...
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...righ t?

Thnks for all your suggestions!!

Nov 10 '06 #2
I would not say that returning a DataReader is not a good practice in
general. If fact the best approach depends upon the problem and issues at
hand.

The DataReader is a read-only, forward-only object that is good for quick
retrieval of data while maintaining an open database connection. You should
close the DataReader BEFORE closing the connection or it will be orphaned and
there will be problems in performing subsequent operations in the same
database. You can use the DataReader directly as a datasource on list
oriented controls. Also, you can iterate through the DataReader and capture
the data to business objects.

The DataTable does not apply to a DataReader. It is usually used in
connection with the DataSet object and the DataAdapter. It is used to
contain a snapshot of the underlying data while DISCONNECTED from the
datasource. You can, however, repeatedly access the data going forward or
backward in the result set and perform updates, insertions and deletions in a
disconnected fashion and then reconnect to the datasource to apply the
changes.

I hope this helps. You should consult the documentation or any of a number
of excellent books on the subject. A good current suggestion is "Programmin g
Microsoft ADO.NET 2.0 - Core Reference, 2005 Edition" by David Sceppa
(Microsoft Press), ISBN 0-7356-2206-X.

Good luck,
Eagle
"Diffident" wrote:
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...righ t?

Thnks for all your suggestions!!
Nov 10 '06 #3
Let's consider that I have a generic method in my DAL which returns
IDataReader object to my BLL. When will I close my connection. Should I close
it in BLL? It does seem to be a good practice to close the connection in
BLL.....

"Ea******@HighF lyingBirds.com" wrote:
I would not say that returning a DataReader is not a good practice in
general. If fact the best approach depends upon the problem and issues at
hand.

The DataReader is a read-only, forward-only object that is good for quick
retrieval of data while maintaining an open database connection. You should
close the DataReader BEFORE closing the connection or it will be orphaned and
there will be problems in performing subsequent operations in the same
database. You can use the DataReader directly as a datasource on list
oriented controls. Also, you can iterate through the DataReader and capture
the data to business objects.

The DataTable does not apply to a DataReader. It is usually used in
connection with the DataSet object and the DataAdapter. It is used to
contain a snapshot of the underlying data while DISCONNECTED from the
datasource. You can, however, repeatedly access the data going forward or
backward in the result set and perform updates, insertions and deletions in a
disconnected fashion and then reconnect to the datasource to apply the
changes.

I hope this helps. You should consult the documentation or any of a number
of excellent books on the subject. A good current suggestion is "Programmin g
Microsoft ADO.NET 2.0 - Core Reference, 2005 Edition" by David Sceppa
(Microsoft Press), ISBN 0-7356-2206-X.

Good luck,
Eagle
"Diffident" wrote:
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...righ t?

Thnks for all your suggestions!!
Nov 10 '06 #4
datareaders use unmanaged memory, so best practice is open, and close them
in the same routine, with a using or try/finally statement.

-- bruce (sqlwork.com)

"Diffident" <Di*******@disc ussions.microso ft.comwrote in message
news:F4******** *************** ***********@mic rosoft.com...
Let's consider that I have a generic method in my DAL which returns
IDataReader object to my BLL. When will I close my connection. Should I
close
it in BLL? It does seem to be a good practice to close the connection in
BLL.....

"Ea******@HighF lyingBirds.com" wrote:
>I would not say that returning a DataReader is not a good practice in
general. If fact the best approach depends upon the problem and issues
at
hand.

The DataReader is a read-only, forward-only object that is good for quick
retrieval of data while maintaining an open database connection. You
should
close the DataReader BEFORE closing the connection or it will be orphaned
and
there will be problems in performing subsequent operations in the same
database. You can use the DataReader directly as a datasource on list
oriented controls. Also, you can iterate through the DataReader and
capture
the data to business objects.

The DataTable does not apply to a DataReader. It is usually used in
connection with the DataSet object and the DataAdapter. It is used to
contain a snapshot of the underlying data while DISCONNECTED from the
datasource. You can, however, repeatedly access the data going forward
or
backward in the result set and perform updates, insertions and deletions
in a
disconnected fashion and then reconnect to the datasource to apply the
changes.

I hope this helps. You should consult the documentation or any of a
number
of excellent books on the subject. A good current suggestion is
"Programmin g
Microsoft ADO.NET 2.0 - Core Reference, 2005 Edition" by David Sceppa
(Microsoft Press), ISBN 0-7356-2206-X.

Good luck,
Eagle
"Diffident" wrote:
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...righ t?

Thnks for all your suggestions!!

Nov 10 '06 #5
You would have to close it there. The DAL would have no idea when the
consumer is done with the datareader. This is why using datareaders is more
risky - you are relying on the class consuming the DAL to close the
connection. If the programmer of that class forgets, you end up spending
time tracking connection leaks.

"Diffident" <Di*******@disc ussions.microso ft.comwrote in message
news:F4******** *************** ***********@mic rosoft.com...
Let's consider that I have a generic method in my DAL which returns
IDataReader object to my BLL. When will I close my connection. Should I
close
it in BLL? It does seem to be a good practice to close the connection in
BLL.....

"Ea******@HighF lyingBirds.com" wrote:
>I would not say that returning a DataReader is not a good practice in
general. If fact the best approach depends upon the problem and issues
at
hand.

The DataReader is a read-only, forward-only object that is good for quick
retrieval of data while maintaining an open database connection. You
should
close the DataReader BEFORE closing the connection or it will be orphaned
and
there will be problems in performing subsequent operations in the same
database. You can use the DataReader directly as a datasource on list
oriented controls. Also, you can iterate through the DataReader and
capture
the data to business objects.

The DataTable does not apply to a DataReader. It is usually used in
connection with the DataSet object and the DataAdapter. It is used to
contain a snapshot of the underlying data while DISCONNECTED from the
datasource. You can, however, repeatedly access the data going forward
or
backward in the result set and perform updates, insertions and deletions
in a
disconnected fashion and then reconnect to the datasource to apply the
changes.

I hope this helps. You should consult the documentation or any of a
number
of excellent books on the subject. A good current suggestion is
"Programmin g
Microsoft ADO.NET 2.0 - Core Reference, 2005 Edition" by David Sceppa
(Microsoft Press), ISBN 0-7356-2206-X.

Good luck,
Eagle
"Diffident" wrote:
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...righ t?

Thnks for all your suggestions!!

Nov 10 '06 #6
I wish I had a dollar for every post I've read where developers use
DataReaders and don't seem to understand that they are holding connections
open, and then they start getting exceptions because the pool has no more
connections available.

There is a bit more overhead in using a SqlDataAdapter to return a Dataset,
but the advantage is that the Adapter handles all the connection business
automatically and now you've got disconnected data that you can do whatever
you want with in your DAL without worrying about blowup up your application.

Word to the wise.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Diffident" wrote:
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...righ t?

Thnks for all your suggestions!!
Nov 11 '06 #7

That's the question on the table ... isn't it?

Do the developers know how to use the IDataReader's correctly?

If so, then the biz layer can consume/use and CLOSE the datareader
correctly.
Aka, you can trust your biz layer developers to do this.

If not, then return DataTables or DataSets, and not IDataReaders.
IDataReader's offer the best performance, but there isn't any magic fairy
dust, if you want use them, you need to know how to use them , and how to
close them.

Where I am, I can trust the bizlayer developers to close the datareaders.
However, presentation developers could be anybody, thus I do NOT (where I
am ) send IDataReaders to people who code the presentation layers.

........
You'll have to decide what is the appropriate solution where you work.

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com wrote in message
news:80******** *************** ***********@mic rosoft.com...
I wish I had a dollar for every post I've read where developers use
DataReaders and don't seem to understand that they are holding connections
open, and then they start getting exceptions because the pool has no more
connections available.

There is a bit more overhead in using a SqlDataAdapter to return a
Dataset,
but the advantage is that the Adapter handles all the connection business
automatically and now you've got disconnected data that you can do
whatever
you want with in your DAL without worrying about blowup up your
application.
>
Word to the wise.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Diffident" wrote:
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...righ t?

Thnks for all your suggestions!!

Nov 13 '06 #8

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

Similar topics

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...
5
5764
by: Jason Huang | last post by:
Hi, Is it possible to bind DataReader to a DataGrid in C# windows form? And how? And can we update data in a DataSet by using the DataReader? Thanks for help. Jason
12
467
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. I find that the second one always is blank. I assume this is because the DataReader is read-forward only. ******************************************************** connect.open() cmdSelect = New OleDbCommand("exec populateCities 'North...
20
5534
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to "clean up" the DataView once it is not referenced and will not effect the number of connections to the database. A DataReader on the other hand always maintains a connection to the database and must be explicitly closed (Do not rely on garbage...
2
1579
by: Andrei Pociu | last post by:
In a typical ASP .NET Web Application (website), I'm currently using a class where I declare some public static objects. For example there's the place where I initialize the SqlConnection. Also there's the place where I declare one public static DataReader, but I'm not sure this is the best way to do it. I use that DataReader in all the webforms and user controls, whenever I have to read data from the db. So, should I do it this way or...
1
3091
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public string get_array(string sql) { //create db connection & open
7
3400
by: Varangian | last post by:
Hi all, the question I want to ask if the conversion of a DataReader to a Table looping through the DataReader is better than using the Fill Method of the DataAdapter... I'm asking because internally the DataAdapter uses the DataReader... so whats the deal of writing a method that converts a DataReader into a DataTable ? Thanks!
3
3386
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the reader and the command object (but don't close the connection which is public in the solution). The first time i open my form from a parent form, there is no problem. Everything works fine. I then close down my form, and I dispose of the form.
0
9589
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
10212
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
10047
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
9995
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
9863
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
8872
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
7410
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
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.