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,
- protected void BinddataTable()
- {
- SqlConnection con = new SqlConnection("your database connection string");
- con.Open();
- SqlCommand cmd = new SqlCommand("Write your query or procedure", con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- da.Fill(dt);
- grid.DataSource = dt;
- grid.DataBind();
- }
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,
- protected void BindDataSet()
- {
- SqlConnection con = new SqlConnection("your database connection string ");
- con.Open();
- SqlCommand cmd = new SqlCommand("Write your query or procedure ", con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- grid.DataSource = ds;
- grid.DataBind();
- }
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,
- protected void Bind()
- {
- SqlConnection con = new SqlConnection("your database connection string ");
- con.Open();
- SqlCommand cmd = new SqlCommand("Write your query or procedure ", con);
- SqlDataReader dr = cmd.ExecuteReader();
- grid.DataSource = dr;
- grid.DataBind();
- }
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,
- protected void Bind()
- {
- SqlConnection con = new SqlConnection("your database connection string ");
- con.Open();
- SqlCommand cmd = new SqlCommand("Write your query or procedure ", con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- grid.DataSource = ds;
- grid.DataBind();
- }
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.