How to Convert DataReader to DataTable

Waqas Anwar
01 December 2010
5026 Views

You have a DataReader object and you want to convert it into DataTable object, You can use DataTable class Load method which accepts DataReader as parameter as shown in following code.

string connString = ConfigurationManager.ConnectionStrings["NorthwindConn"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string query = "SELECT * FROM Customers";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();

dt.Load(dr);

conn.Close();