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



Leave a Reply

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

You are commenting using your 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 )

Connecting to %s