Convert DataReader to DataTable in C#

SqlDataReader sdr = comm.ExecuteReader();

 
 

DataTable ret = new DataTable();

 
 

DataTable schema = sdr.GetSchemaTable();

for (int n = 0; n < schema.Rows.Count; n++) ret.Columns.Add(new DataColumn((String) schema.Rows[n][“ColumnName”], (Type) schema.Rows[n][“DataType”]));

 
 

while (sdr.Read())

{

DataRow dr = ret.NewRow();

for (int n = 0; n < sdr.FieldCount; n++) dr[n] = sdr.GetValue(n);

ret.Rows.Add(dr);

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s