码迷,mamicode.com
首页 > 数据库 > 详细

convert SqlDataReader to DataTable

时间:2018-11-07 14:03:45      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:row   object   err   stat   tab   ble   pre   nload   span   

public static DataTable ConvertDataReaderToDataTable(SqlDataReader reader)
{
    try
    {
        DataTable objDataTable = new DataTable();
        int intFieldCount = reader.FieldCount;
        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;
    }
    catch (Exception ex)
    {
        throw new Exception("Convert Error!", ex);
    }
}

convert SqlDataReader to DataTable

标签:row   object   err   stat   tab   ble   pre   nload   span   

原文地址:https://www.cnblogs.com/jizhiqiliao/p/9922030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!