• datareader转换成datatable【转】


    别人的参考:

     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();
                    }
                }

  • 相关阅读:
    Struts2文件上传和下载
    Struts2自定义类型转换器
    struts2数据处理的几种方式
    Struts2常量_Action配置路径_通配符
    Struts框架
    struts入门
    Log4J日志组件
    处理文件上传与下载
    文件上传
    国际化和本地化
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/3108743.html
Copyright © 2020-2023  润新知