dataReader转换成Datatable,中间还有数据的类型
/// <summary>
/// 用于把dataReader转换成DataTable
/// </summary>
/// <param name="dataReader"></param>
/// <returns></returns>
public DataTable DataReaderToDataTable(DbDataReader dataReader)
{
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
try
{
foreach (DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn(myRow[0].ToString(),System.Type.GetType(myRow["DataType"].ToString()));
datatable.Columns.Add(myDataColumn);
}
while (dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for (int i = 0; i < schemaTable.Rows.Count; i++)
{
myDataRow[i] = dataReader[i];
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
dataReader.Close();
return datatable;
}
catch (Exception ex)
{
throw new Exception("error", ex);
}
}