Load DataTable From SqlDataReader

  1. using System.Data;  
  2. using System.Configuration;  
  3. using System.Data.SqlClient;  
  4. protected void ConvertDataReaderToDataTable(object sender, EventArgs e) {  
  5.     string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
  6.     using(SqlConnection con = new SqlConnection(constr)) {  
  7.         using(SqlCommand cmd = new SqlCommand("SELECT * FROM Customers")) {  
  8.             cmd.Connection = con;  
  9.             con.Open();  
  10.             using(SqlDataReader sdr = cmd.ExecuteReader()) {  
  11.                 //Create a new DataTable.  
  12.                 DataTable dtCustomers = new DataTable("Customers");  
  13.                 //Load DataReader into the DataTable.  
  14.                 dtCustomers.Load(sdr);  
  15.             }  
  16.             con.Close();  
  17.         }  
  18.     }