将DataReader转换为DataTable
/// <summary>
/// 将DataReader转换为DataTable
/// </summary>
/// <param name="reader">要被转换的DataReader</param>
/// <returns>转换后的DataTable</returns>
public static DataTable ConvertDataReaderToDataTable(IDataReader reader)
{
DataTable objDataTable = new DataTable();
int intFieldCount = reader.FieldCount;
//将列名添加到DataTable中
for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
{
objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
}
//装入内容
objDataTable.BeginLoadData();
object[] objValues = new object[intFieldCount];
while (reader.Read())
{
reader.GetValues(objValues);
objDataTable.LoadDataRow(objValues, true);
}
reader.Close();
objDataTable.EndLoadData();
return objDataTable;
}
/// 将DataReader转换为DataTable
/// </summary>
/// <param name="reader">要被转换的DataReader</param>
/// <returns>转换后的DataTable</returns>
public static DataTable ConvertDataReaderToDataTable(IDataReader reader)
{
DataTable objDataTable = new DataTable();
int intFieldCount = reader.FieldCount;
//将列名添加到DataTable中
for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
{
objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
}
//装入内容
objDataTable.BeginLoadData();
object[] objValues = new object[intFieldCount];
while (reader.Read())
{
reader.GetValues(objValues);
objDataTable.LoadDataRow(objValues, true);
}
reader.Close();
objDataTable.EndLoadData();
return objDataTable;
}