.Net

DataSet Vs DataReader

DataSet and DataReader are widely used to get/fetch the data from the database.

  • DataReader is used to retrieve read-only and forward-only data from a database. It provides the ability to expose the data from database while DataSet is a collection of in-memory tables. 
  • DataReader fetches the records from database and stores in the network buffer and gives whenever requests. It releases the records as query executes and do not  wait for the entire query to execute. Hence very fast as compare to the DataSet which releases the data after loading all the data in memory.
  • DataReader fetches data from a single table while DataSet can fetch data from multiple tables.
  • DataReader is read only so no transaction like insert, update and delete is possible while these transactions are possible in DataSet.
  • DataReader is forward only. It retrieves one row at a time. At one row at a time is stored data in memory in datareader. It increases the performance of application. When you are using datareader you can not move back and can’t fetch random record. DataReader fetches data from single table.
  • Dataset is disconnected oriented architecture. It means there is no need of active database connection during when you are working with datasets. Dataset is collection of DataTables. Dataset object contain multiple rowsets from the same data source as well as from the relationship between them. Dataset can save data as xml and also load data from xml DataSource.

DataReader

            using (SqlConnection con = new SqlConnection(Connection String))
            {
                try
                {
                    con.Open();
                    SqlCommand sqlCommand = new SqlCommand("Select * from 
                    StudentMaster", con);
                    SqlDataReader reader= null;
                    reader = sqlCommand.ExecuteReader();
                    if (reader.HasRows)
                    {
                        datagridView.DataSource = reader;
                        datagridView.DataBind();
                    }
                }
                catch (SqlException ex)
                {
                    throw ex;
                }                
            }

Dataset

            using (SqlConnection con= new SqlConnection(Connection String))
            {
                try
                {
                    con.Open();
                    SqlCommand command= new SqlCommand("Select * from 
                    StudentMaster", con);
                    SqlDataAdapter adp= new SqlDataAdapter(command);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    if(ds.Tables[0].Rows.Count>0)
                    {
                        dataGridVIew.DataSource = ds;
                        dataGridVIew.DataBind();
                    }
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
            }

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button