return View(ConverDataReaderToDataTable(reader));
// 静态方法 public static DataTable ConverDataReaderToDataTable(IDataReader reader)
{
// 检查外部传入的参数
if (null == reader)
return null;
// 实例化一个DataTable
DataTable dataTable = new DataTable();
dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
int fieldCount = reader.FieldCount;
// 在表中创建字段
for (int counter = 0; counter < fieldCount; counter++)
{
dataTable.Columns.Add(reader.GetName(counter), reader.GetFieldType(counter));
}
dataTable.BeginLoadData();
object[] values = new object[fieldCount];
while (reader.Read())
{
// 添加行
reader.GetValues(values);
dataTable.LoadDataRow(values, true);
}
// 完成转换并返回
dataTable.EndLoadData();
return dataTable;
}
html显示:
@model System.Data.DataTable
<table>
<tr>
@for (int i = 0; i < Model.Columns.Count; i++)
{
<td>@Model.Columns[i].ColumnName</td>
}
</tr>
@for (int i = 0; i < Model.Rows.Count; i++)
{
<tr>
@for(int j = 0; j < Model.Rows[i].ItemArray.Count(); j++)
{
<td>@Model.Rows[i].ItemArray[j]</td>
}
</tr>
}
</table>
|
请发表评论