SqlDataReader 结果集 转成 DataTable

时间:2022-05-04 18:12:38
/// <summary>
/// SqlDataReader 转成 DataTable
/// 源需要是结果集
/// </summary>
/// <param name="dataReader"></param>
/// <returns></returns>
private static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
///定义DataTable
DataTable datatable = new DataTable(); try
{ ///动态添加表的数据列
for (int i = ; i < dataReader.FieldCount; i++)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = dataReader.GetFieldType(i);
myDataColumn.ColumnName = dataReader.GetName(i);
datatable.Columns.Add(myDataColumn);
} ///添加表的数据
while (dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for (int i = ; i < dataReader.FieldCount; i++)
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
///关闭数据读取器
dataReader.Close();
return datatable;
}
catch (Exception ex)
{
///抛出类型转换错误
//SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
}