Home Asp.net Difference: A Complete Guide DataSet vs DataReader in .NET

Difference: A Complete Guide DataSet vs DataReader in .NET

101
0
Redirect Page on Button Click

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

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.

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

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

That’s it! I hope you get the difference between DataSet and DataReader in .NET by step by step guide provided by me. If you’ve any doubts or questions, don’t hesitate to comment below. I would be happy to provide my feedback on it. Thank you!

Previous articleDifference: A Complete Guide DataSet vs DataTable in .NET
Next articleDifference Between DataSet, DataReader, DataAdapter, DataView
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

13 − thirteen =