Difference between DataReader, Dataset, DataTable and DataAdapter in ASP.NET

DataReader:

It is read only format, we can't update records.

It is connection oriented, whenever data bind from database that need connection and then connection is disconnected.

Code Behind:

  1. protected void Bind()  
  2. {  
  3.    SqlConnection con = new SqlConnection("Data Source=database;Integrated Security=true;Initial Catalog=catalog");  
  4.    {  
  5.       con.Open();  
  6.       SqlCommand cmd = new SqlCommand("Select record FROM table", con);  
  7.       SqlDataReader dr = cmd.ExecuteReader();  
  8.       grdvw.DataSource = dr;  
  9.       grdvw.DataBind();  
  10.       con.Close();  
  11.    }  

Dataset:

It is connectionless oriented.

Whenever we bind data from database. It connects indirectly to the database and then disconnected. Its easily read and write data from database.

Code Behind:

  1. protected void Bind()  
  2. {  
  3.    SqlConnection con = new SqlConnection("Data Source=datasource;Integrated Security=true;Initial Catalog=catalog");  
  4.    con.Open();  
  5.    SqlCommand cmd = new SqlCommand("select record from table", con);  
  6.    SqlDataAdapter da = new SqlDataAdapter(cmd);  
  7.    DataSet ds = new DataSet();  
  8.    da.Fill(ds);  
  9.    grdvw.DataSource = ds;  
  10.    gvdvw.DataBind();  

DataTable:

DataTable represents a single table in the database.

It has rows and columns. There is no much difference between dataset and datatable, Dataset is simply the collection of datatables.

Code Behind:

  1. protected void Bind()  
  2. {  
  3.    SqlConnection con = new SqlConnection("Data Source=datasource;Integrated Security=true;Initial Catalog=catalog");  
  4. con.Open();  
  5.    SqlCommand cmd = new SqlCommand("select record from table", con);  
  6.    SqlDataAdapter da = new SqlDataAdapter(cmd);  
  7.    DataTable dt = new DataTable();  
  8.    da.Fill(dt);  
  9.    grdvw.DataSource = dt;  
  10.    gvdvw.DataBind();  

DataAdapter:

Dataadapter is a disconnected oriented architecture.

DataAdapter is like a mediator between DataSet (or) DataTable and database. This dataadapter is used to read the data from database and bind to dataset.

  1. .cs page  
  2. protected void Bind()  
  3. {  
  4.    SqlConnection con = new SqlConnection("Data Source=datasource;Integrated Security=true;   Initial Catalog=catalog");  
  5.    con.Open();  
  6.    SqlCommand cmd = new SqlCommand("select record from table", con);  
  7.    SqlDataAdapter da = new SqlDataAdapter(cmd);  
  8.    DataSet ds = new DataSet();  
  9.    da.Fill(ds);  
  10.    grdvw.DataSource = ds;  
  11.    gvdvw.DataBind();  
  12. }
Ebook Download
View all
Learn
View all