首页 > 数据库技术 > 详细

convert SqlDataReader to DataTable

时间:2018-11-07 13:51:16      阅读:169      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!