ADO.NET Technique With ASP.NET

In this article I will explain the following ADO.NET concepts: DataTable, DataSet, DataReader and DataAdapter. Also, learn about some differences between these ADO.NET concepts.

DataTable:

  • DataTable represents a single table in a database.
  • In this show row and column.
  • DataSet is a collection of data tables.
  • In this store data record.

DataTable representation in .aspx.cs code,

  1. protected void BinddataTable()   
  2. {  
  3.     SqlConnection con = new SqlConnection("your database connection string");  
  4.     con.Open();  
  5.     SqlCommand cmd = new SqlCommand("Write your query or procedure", con);  
  6.     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  7.     DataTable dt = new DataTable();  
  8.     da.Fill(dt);  
  9.     grid.DataSource = dt;  
  10.     grid.DataBind();  
  11. }  
DataSet

 

  • DataSet can hold multiple tables at a time.
  • DataSet allow data access to easily read or write data operations in / from the database.
  • DataSet data stores in local system.
  • DataSet hold multiple rows at a time.
  • DataSet use more memory.
  • DataSet maintain relation.
  • DataSet bind data from the database.

DataSet representation in .aspx.cs code,

 

  1. protected void BindDataSet()   
  2. {  
  3.     SqlConnection con = new SqlConnection("your database connection string ");  
  4.     con.Open();  
  5.     SqlCommand cmd = new SqlCommand("Write your query or procedure ", con);  
  6.     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  7.     DataSet ds = new DataSet();  
  8.     da.Fill(ds);  
  9.     grid.DataSource = ds;  
  10.     grid.DataBind();  
  11. }  
DataReader

 

  • DataReader hold only one table at a time.
  • DataReader only provide read only access mode and cannot write data.
  • DataReader not required local storage to data store.
  • DataReader hold one row at time.
  • DataReader uses less memory.
  • DataReader do not maintain relation.

DataReader representation in .aspx.cs code,

  1. protected void Bind()   
  2. {  
  3.     SqlConnection con = new SqlConnection("your database connection string ");  
  4.     con.Open();  
  5.     SqlCommand cmd = new SqlCommand("Write your query or procedure ", con);  
  6.     SqlDataReader dr = cmd.ExecuteReader();  
  7.     grid.DataSource = dr;  
  8.     grid.DataBind();  
  9. }  
DataAdapter

 

  • DataAdapter is a disconnected oriented architecture.
  • DataAdapter is one type of bridge between dataset and database.
  • DataAdapter is used to read the data from DataTable and bind to DataSet.

DataAdapter representation in .aspx.cs code,

  1. protected void Bind()   
  2. {  
  3.     SqlConnection con = new SqlConnection("your database connection string ");  
  4.     con.Open();  
  5.     SqlCommand cmd = new SqlCommand("Write your query or procedure ", con);  
  6.     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  7.     DataSet ds = new DataSet();  
  8.     da.Fill(ds);  
  9.     grid.DataSource = ds;  
  10.     grid.DataBind();  
  11. }  
In this article we saw some basic information about different ADO.NET concepts. It is also asked in many interviews for freshers and experienced developers.

 


Similar Articles