How to Convert DataReader to DataTable


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();

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments