Convert SqlDataReader to DataTable c#
By Mash B
Copy data from SqlDatareader to DataTable using Load function of DataTable.
Ex.
using(SqlConnection conn = new SqlConnection(YourConnectionString))
{
String sqlQuery = "Select * from Emp";
// open the connection
conn.Open();
using(SqlCommand cmd = new SqlCommand(sqlQuery,conn))
{
// execute query and read data in SqlDataReader
using(SqlDataReader reader = cmd.ExecuteReader())
{
DataTable dt = new DataTable();
// call load method of datatable to copy content of reader
dt.Load(reader); // Load method does not close the connnection. In this case, the using statement does.
}
}
}
Convert SqlDataReader to DataTable c# (7258 Views)