using System.Data;
using System.Configuration;
using System.Data.SqlClient;
protected void ConvertDataReaderToDataTable(object sender, EventArgs e) {
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using(SqlConnection con = new SqlConnection(constr)) {
using(SqlCommand cmd = new SqlCommand("SELECT * FROM Customers")) {
cmd.Connection = con;
con.Open();
using(SqlDataReader sdr = cmd.ExecuteReader()) {
//Create a new DataTable.
DataTable dtCustomers = new DataTable("Customers");
//Load DataReader into the DataTable.
dtCustomers.Load(sdr);
}
con.Close();
}
}
}In below example we will see how to load DataTable from SqlDataReader. At first we have to read data into SqlDataReader object then we will dump all data using Load() method. Have a look on below example.
