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
@Model.Columns[i].ColumnName | }
@Model.Rows[i].ItemArray[j] | }