Solved

DataReader vs. DataTable

Posted on 2011-02-10
9
597 Views
Last Modified: 2012-05-11
Hello,

I have the following code listed below and would like to know which method would be better to use, either a DataTable or DataReader. The content that this code retrieves will hardly change but it's data is tied to other data for reasons that I cannot make it static within the site.

This code below is called in 20 different pages. The same code in each 20 pages called each time a user visits each of those 20 pages.

CODE:

    protected void Page_Load(object sender, EventArgs e)
    {
            Retrieve_NavLinks();
    }

    protected void Retrieve_NavLinks()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString))
        {
            SqlCommand cmdRetrieveNavLinks = new SqlCommand();
            cmdRetrieveNavLinks.CommandText = "PicksPhoto_RetrieveNavLinks";
            cmdRetrieveNavLinks.CommandType = CommandType.StoredProcedure;
            cmdRetrieveNavLinks.Connection = conn;

            conn.Open();

            SqlDataReader rdr = cmdRetrieveNavLinks.ExecuteReader();

            rpRetrieveNavLinks.DataSource = rdr;
            rpRetrieveNavLinks.DataBind();
        }
    }


STORED PROCEDURE USED:

ALTER PROCEDURE [dbo].[PicksPhoto_RetrieveNavLinks]

AS

SELECT navm.mnav_id, navm.onav_id, navm.mnav_linkname, navm.mnav_url
FROM [App_NavMain] AS navm
INNER JOIN [App_NavOrder] navo
ON navm.onav_id = navo.onav_id
WHERE navm.mnav_id > 1
ORDER BY onav_id ASC

GO
0
Comment
Question by:asp_net2
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 34867281
A DataReader maintains a connection to the database for the entire time you are reading the data, and the data is delivered as you ask for it. A DataTable pulls all the data from the database into the DataTable construct and then lets you do all sorts of things to it.

Generally a DataTable will be slower and more memory intensive.

In your situation could you consider caching the data for a period of time to avoid having to pull it every page load?
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34867312
OK, I'm not quite sure about other complications for your app but I'd probably (especially for ASP) I'd choose Datareader.

The main and very important advantage - performance. I believe for on-line apps it's crucial.

It's easier to work with tables, definitely.

Probably, volue of data is important. If it's small - DataTable would be OK and fast enough.

Also. datareaders have other drawback. Please read the article:

http://www.eggheadcafe.com/articles/20030205.asp
0
 
LVL 4

Author Comment

by:asp_net2
ID: 34867533
Could you show me example how I would cache the code I have above? Is there anything I have to enable for caching?

I guess I'm a little confused, I was hoping of a solution that would somehow prevent 20 hits to my DB for the same data as in the code above.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 34867568
Well your question said "would be better to use, either a DataTable or DataReader" - which has been answered.

It sounds like you also want to know how to improve the performance of the site. Firstly it depends how often and under what circumstances the data changes. If having considered that you determine that you can afford to cache the data for a period then you need to consider it the data the same for all users in which case look at asp.net caching (http://msdn.microsoft.com/en-us/library/6hbbsfk6(v=VS.71).aspx) if it differs from user to user then consider using a session variable (http://msdn.microsoft.com/en-us/library/6ad7zeeb.aspx).

HTH
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 4

Author Comment

by:asp_net2
ID: 34867601
The data will hardly ever change. All users need to see the same data. I plan on expecting thousands of hits per day.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34867750
OK, this definitely needs caching to avoid DB traffic, I'd say. Not quite sure how this is done in ASP correctly - should be on Google.
0
 
LVL 14

Accepted Solution

by:
systan earned 500 total points
ID: 34868566
if where you are comfortable to use your code?, then go for it.

Using dataReader is faster and a direct memory use.
Using dataTable can be filled if another data component to be called, like, dataRow, or dataAdapter.

As long as I remember when I benchmark these to component, it was a 2 seconds difference in a hundred thousand records.

DataReader vs. DataTable ?
Winner is DataReader

BUT,
like I said "where you are comfortable to use your code?, then go for it."
it only matter a second or 2 or maybe 3 for a million records.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34870546
0
 
LVL 14

Expert Comment

by:systan
ID: 34871329
The best way to optimized your web performance is the way you handle your configurations,  and the code structures you wrote if is clean and simple.



thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now