ASP.NET DataReader vs. DataSet

I've recently renewed my effort to "use the right tool for the job" when developing xhtml and CSS, which got me to thinking. If using a <ul> rather than a <div> is better in a certain application, are there better ASP.NET tools for other jobs?

With that mindset I came across a task that required connecting to one of our databases. Rather than building a DAL (and no, I'm not LINQified yet) I decided to just grab the data though a SqlConnection and go from there. So far so good. I then was faced with how to "deal" with the stuff returned from the SqlCommand. Here ASP.NET has a couple controls that do can read the data from your SqlCommand: DataReader and DataSet. But which to use?

I've always been partial to using what you know, and knowing what you use. So for almost all my cases I've used the DataSet control. This has allowed me to not only get stuff out of a database, but update and insert stuff as well. In those cases, DataSet is ideal. But, with all that extra functionality comes extra overhead.

DataReader, on the other hand, doesn't "do everything for you" and then some. It does its job, and does it well, and doesn't do anything else (sound like a UNIX concept, doesn't it?). With DataReader you are doing just that, reading the data from the database, and handling it (parsing it into an object, writing it onto the page, etc.), and closing the SqlConnection.

For details and a chart that illustrates the point, see my article on JoeTheWebGuy.net.

5 Comments

  • "you can easily see why you'd want to use a DataReader over a DataSet (again, unless you need the added features that DataSet gives you)."

    Not really. I can easily see that blue is way faster than pink, though!

  • 10 seconds to catch 1000 records? Please, this graph is just not believable, at least not on modern machines. I don't know what is happening on that test - maybe the machine just runs out of memory?
    I am aware that DataSets are sometimes overkill. I also saw a lot of worse code using data readers in the past though. DataSets is not just about updates and inserts, it is also a powerful cache and can help you to save a lod ove unnecessary round trips.

  • In ASP.NET, when are you really going to present 1000+ records to the GUI?

  • when would you stream more than 100 records to browser in asp.net?

  • I think its not 1 record at a time.. it fetches bunch of records at client side. we can say it caches the data in client's memory.. depending upon the data size .. number of columns used n all...

Comments have been disabled for this content.