要将OleDbDataReader查询结果转换为DataTable,可以按照以下步骤进行操作:
DataTable dataTable = new DataTable();
DataTable schemaTable = reader.GetSchemaTable();
foreach (DataRow row in schemaTable.Rows)
{
DataColumn column = new DataColumn();
column.ColumnName = row["ColumnName"].ToString();
column.DataType = (Type)row["DataType"];
dataTable.Columns.Add(column);
}
while (reader.Read())
{
DataRow dataRow = dataTable.NewRow();
for (int i = 0; i < reader.FieldCount; i++)
{
dataRow[i] = reader.GetValue(i);
}
dataTable.Rows.Add(dataRow);
}
完整示例代码如下:
DataTable dataTable = new DataTable();
DataTable schemaTable = reader.GetSchemaTable();
foreach (DataRow row in schemaTable.Rows)
{
DataColumn column = new DataColumn();
column.ColumnName = row["ColumnName"].ToString();
column.DataType = (Type)row["DataType"];
dataTable.Columns.Add(column);
}
while (reader.Read())
{
DataRow dataRow = dataTable.NewRow();
for (int i = 0; i < reader.FieldCount; i++)
{
dataRow[i] = reader.GetValue(i);
}
dataTable.Rows.Add(dataRow);
}
这样,OleDbDataReader查询结果就会转换为DataTable对象。