Wednesday, May 27, 2015

Difference between DataSet and DataReader

Introduction

In this post I will try to explain the difference between DataSet and DataReader. My previous post Difference between stored procedure and user defined functions.

DataSet

DataSet is a type of disconnected architecture which means sql live database connections is not required while fetch the data from database. DataSet contains the collection of multiple DataTables. We are able to keep the tables, views values from database in DataSet. Also we can fetch the XML values and store in DataSet. We can use DataAdapter as a mediator between DataSet and SQL DataBase tables.

public DataSet GetData()
{
    SqlConnection conString = new SqlConnection("DataBase Connection");
    conString.Open();
    SqlCommand cmdQuery = new SqlCommand("SQL Statement", conString);
    SqlDataAdapter sda = new SqlDataAdapter(cmdQuery);
    DataSet dsData = new DataSet();
    sda.Fill(dsData);
    return dsData;
}


DataReader

DataReader is also used to fetch the data from SQL database. When compared to DataSet, DataReader is a connection oriented architecture. Also it is a read/forward only approach while fetching the data from SQL database. We can use DataReader for fast fetching the data from SQL database. DataReader will loop one by one record and return the data. Here we will see a sample for binding the GridView using DataReader.

Protected void BindDataGrid()
{
    SqlConnection conString = new SqlConnection("DataBase Connection");
    conString.Open();
    SqlCommand cmdQuery = new SqlCommand("SQL Statement", conString);
    SqlDataReader sdrData = cmdQuery.ExecuteReader();
    gvEmployees.DataSource = sdrData;
    gvEmployees.DataBind();
    conString.Close();
}


Output
In this post i tried to explain the difference between SQL Stored procedure and functions. My previous post Difference between stored procedure and user defined functions.

5 comments:

  1. I have been following you for a couple of months now but this is my first time commenting on a blog post. Thank you for sharing your knowledge and experience with us. Keep up the good work. Already bookmarked for future reference.

    SAP training in Chennai

    ReplyDelete
  2. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


    Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

    ReplyDelete

Followers