路漫漫,求索不息

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

别人的参考:

 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("转换出错!", ex);
        }

    }

 

 

实际应用中的:

using (var dr = SqlHelper.ExecuteReader(_dataBase, GetOrderOutPutStatisticsSql(queryModel, ref sqlCount) + HotelDBConfig.GetSqlComments("酒店客服系统", "订单产量统计")))
            {
                if (dr != null)
                {
                    DataTable table = new DataTable();
                    int intFieldCount = dr.FieldCount;
                    for (int i = 0; i < intFieldCount; ++i)
                    {
                        table.Columns.Add(dr.GetName(i),dr.GetFieldType(i));
                    }
                    table.BeginLoadData();
                    object[] objectValues=new object[intFieldCount];
                    while (dr.Read())
                    {
                        dr.GetValues(objectValues);
                        table.LoadDataRow(objectValues,true);
                    }
                    dr.Close();
                    table.EndLoadData();
                }
            }

posted on 2013-05-30 18:11  路漫漫,求索不息  阅读(237)  评论(0编辑  收藏  举报