AspnetO

We code, that works!

  • Home
  • Asp.net
  • MVC
  • Interview Questions

Difference: A Complete Guide DataSet vs DataReader in .NET

By: Mayank Modi | Folls In: Asp.net, C#, Interview Questions, VB | Last Updated: Sep 03, 2014

In my previous tutorials, I’d explained the difference between dataset and datatable, difference between String and StringBuilder, difference between executereader executenonquery and executescalar, OOP concepts with examples, difference between dataset datareader dataadapter and dataview and other similar tutorials on difference here.

Now here in this tutorial, I’ll explain the main difference between dataset and datareader as well as show you how to use dataset or datareader with an example in asp.net using c# and vb.net.

Let’s start with the difference between DataSet vs DataTable.

Difference: DataSet vs DataReader

Following are the list of possible differences between DataSet vs DataReader:

DataSet DataReader
It has read/write access, we can update records It has read-only access, we can’t update records
It supports both forward and backward scanning of data It supports forward-only scanning of data
It supports multiple data tables from different databases It only supports a single table of one database
It is connection-less oriented, that means whenever we bind data from database it connects indirectly to the database and then disconnected It is connection oriented, that means whenever we bind data from database it must require a connection and after that disconnected from the connection
You have to be careful about how much data you pull into a DataSet, because this is an in-memory representation It is scalable to any number of records, at least in terms of memory pressure, since it only loads one record at a time
You can Fill a DataSet using the Fill() method of a DataAdapter One typical way to get a DataReader is by using the ExecuteReader() method of a DbCommand
Greater overhead to enable additional features Light-weight object with very little overhead
Slower access to data Faster access to data
Quick Note: If you need read-only access to query results, then go for DataReader because it contains single table which requires low overhead. On the other hand, if you want to perform read/update/delete operation, you must need to go for DataSet for multiple related database tables OR use DataTable for single database table.

The biggest disadvantage of DataSet is speed because it is a high resource consuming process with one or more related database tables are in-memory. It carries considerable overhead because of the related tables, constraints, and relationships among the database tables.

Using DataSet in Asp.net with Example

Example [C#]:

//specify your connection string here..
public static string strConn =
@"Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB";
protected void Page_Load(object sender, EventArgs e)
{
       if (!IsPostBack)
       {
               BindGridviewFileData();
       }
}

//bind subject details to gridview
private void BindGridviewFileData()
{
       try
       {
               using (SqlConnection sqlConn = new SqlConnection(strConn))
               {
                       using (SqlCommand sqlCmd = new SqlCommand())
                       {
                               sqlCmd.CommandText = "SELECT * FROM SubjectDetails";
                               sqlCmd.Connection = sqlConn;
                               sqlConn.Open();
                               SqlDataAdapter objDataAdapter = new SqlDataAdapter(sqlCmd);
                               DataSet objDataSet = new DataSet();
                               objDataAdapter.Fill(objDataSet);
                               gvSubjectDetails.DataSource = objDataSet;
                               gvSubjectDetails.DataBind();
                               sqlConn.Close();
                       }
               }
       }
       catch { }
}

Example [Vb.net]:

'specify your connection string here..
Public Shared strConn As String =
"Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
       If Not IsPostBack Then
               BindGridviewFileData()
       End If
End Sub

'bind subject details to gridview
Private Sub BindGridviewFileData()
       Try
               Using sqlConn As New SqlConnection(strConn)
                       Using sqlCmd As New SqlCommand()
                               sqlCmd.CommandText = "SELECT * FROM SubjectDetails"
                               sqlCmd.Connection = sqlConn
                               sqlConn.Open()
                               Dim objDataAdapter As New SqlDataAdapter(sqlCmd)
                               Dim objDataSet As New DataSet()
                               objDataAdapter.Fill(objDataSet)
                               gvSubjectDetails.DataSource = objDataSet
                               gvSubjectDetails.DataBind()
                               sqlConn.Close()
                       End Using
               End Using
       Catch
       End Try
End Sub

Using DataReader in Asp.net with Example

Example [C#]:

//specify your connection string here..
public static string strConn =
@"Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB";
protected void Page_Load(object sender, EventArgs e)
{
       if (!IsPostBack)
       {
               BindGridviewFileData();
       }
}

//bind subject details to gridview
private void BindGridviewFileData()
{
       try
       {
               using (SqlConnection sqlConn = new SqlConnection(strConn))
               {
                       using (SqlCommand sqlCmd = new SqlCommand())
                       {
                               sqlCmd.CommandText = "SELECT * FROM SubjectDetails";
                               sqlCmd.Connection = sqlConn;
                               sqlConn.Open();
                               SqlDataReader objDataReader = sqlCmd.ExecuteReader();
                               gvSubjectDetails.DataSource = objDataReader;
                               gvSubjectDetails.DataBind();
                               sqlConn.Close();
                       }
               }
       }
       catch { }
}

Example [Vb.net]:

'specify your connection string here..
Public Shared strConn As String =
"Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
       If Not IsPostBack Then
               BindGridviewFileData()
       End If
End Sub

'bind subject details to gridview
Private Sub BindGridviewFileData()
       Try
               Using sqlConn As New SqlConnection(strConn)
                       Using sqlCmd As New SqlCommand()
                               sqlCmd.CommandText = "SELECT * FROM SubjectDetails"
                               sqlCmd.Connection = sqlConn
                               sqlConn.Open()
                               Dim objDataReader As SqlDataReader = sqlCmd.ExecuteReader()
                               gvSubjectDetails.DataSource = objDataReader
                               gvSubjectDetails.DataBind()
                               sqlConn.Close()
                       End Using
               End Using
       Catch
       End Try
End Sub

Signup Today And Get Latest Tutorials For Free!

Subscribe to us and get free latest tutorials notifications whenever we publish a new contents.

About Mayank Modi

Mayank is a web developer and designer who specializes in back-end as well as front-end development. He's a Founder & Chief Editor of AspnetO. If you'd like to connect with him, follow him on Twitter as @immayankmodi.

Share Your Comments & Feedback Cancel reply

Note: To post any code in comment, use <pre>your code</pre>

Social Connections

  • 0 Fans
  • 3,222 Followers
  • 21 Followers
  • 52 Followers
  • 1,559 Subscribers

Top Posts

  • CSS3 Transition: fadeIn and fadeOut like Effects to Hide Show Elements
  • Top 10 OOPS Concepts In C# .NET With Examples
  • Parser Error While Deploying Website to Server in Asp.net
  • Asp.net TextBox: How to Get Set TextBox Value or Text in jQuery
  • How to Print Asp.net GridView Data on Button Click using Javascript?

Find by Tags

Ado.net Ajax appSettings Asp.net C# CheckBox CheckBoxList ConnectionStrings Control CSS CSS3 Difference Download DropDownList Export Facebook fadeIn fadeOut fadeTo fadeToggle File File Extension FileUpload Function GridView IIS Interview Questions JavaScript jQuery MVC OOP RadioButtonList RDP Repeater Send Mail Solutions Split SQL Stored Procedure TextBox Upload Validation VB Web.config Web Hosting

The Man Behind AspnetO

Mayank Modi

Hi there,

Myself Mayank Modi, a Full Stack Developer (.NET Stack) and a blogger from Surat, India.

I'm welcoming you to my blog - AspnetO, a programmers community blog where we code, that works!

I started AspnetO as a hobby and now we're growing day by day. We're now having 5000+ programmers that get benefits and learn new things about website design and development under our community blog.

Here at AspnetO, I write about Beginners to Advance level of tutorials on programming languages like Asp.net using C# and Vb.net, MVC, SQL Server, JavaScript, jQuery etc. In sort, all about .NET Framework and website development stuff and sometimes sharing tips and tricks that can help you to grow up your programming skills.

You can get more details about me and my blog at About us page.

Subscribe To Newsletter

Enter your email address to subscribe to this blog and receive notifications of new posts right to your inbox

Join 1000+ other subscribers

Hot on AspnetO

Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 7245 downloads 39.76 KB
Download This Example
Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 6211 downloads 39.76 KB
Download This Example
Icon
Gridview Insert Update Delete Example in Asp.net, C#, Vb.net 6207 downloads 39.76 KB
Download This Example
Icon
Export gridview all rows or selected rows to word, excel, text, pdf examples 3056 downloads 1.01 MB
Download This Example
Icon
Export gridview all rows or selected rows to word, excel, text, pdf examples 2993 downloads 1.01 MB
Download This Example

Copyright © 2014 - 2019 · All Rights Reserved.replica cartier watches

About | Copyrights | Privacy | Terms | Contact | Advertise | Sitemap
Previous Difference: A Complete Guide DataSet vs DataTable in .NET
Next Difference Between DataSet, DataReader, DataAdapter, DataView in .NET