Home » CodeDigest


How to Convert/Copy SqlDataReader object to DataTable in C#, ADO.Net?
Submitted By Satheesh Babu B
On 6/30/2010 9:48:01 AM
Tags: CodeDigest  

How to Convert SqlDataReader object to DataTable in C#, ADO.Net?


This is a very simple tip which will help us to convert an ADO.Net SqlDataReader object to DataTable object in C#.

The Load(DataReader) method packed with the DataTable object will do the trick for us.

 Refer the below code where the datareader object is passed to Load method of DataTable.

public DataTable GetData()
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand com = new SqlCommand("SELECT [emp_id], [fname], [lname], [job_id], [hire_date] FROM [employee] ORDER BY [emp_id], [fname]", con);
        DataTable dt = new DataTable();
        SqlDataReader dr = com.ExecuteReader();
        return dt;

Include System.Data and System.Data.SqlClient namespace for the above code to work.

Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..