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 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 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 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 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.