public DataTable DataReaderToDataTable(IDataReader reader)
{
DataTable tb = new DataTable();
DataColumn col;
DataRow row;
int i;
for (i = 0; i < reader.FieldCount; i++)
{
col = new DataColumn();
col.ColumnName = reader.GetName(i);
col.DataType = reader.GetFieldType(i);
tb.Columns.Add(col);
}
while (reader.Read())
{
row = tb.NewRow();
for (i = 0; i < reader.FieldCount; i++)
{
row[i] = reader[i];
}
tb.Rows.Add(row);
}
return tb;
}
ASP.NET2.0有更简单的方法:
DataTable dt=new DataTable();
dt.Load(IDataReader);