示例#1
0
        /// <summary>
        /// 执行查询SQL,返回列表object
        /// 前置条件: 必须是映射的SQL语句,且必须指定SQL语句的返回参数类
        /// </summary>
        /// <param name="sqlName"></param>
        /// <param name="pageSize"></param>
        /// <param name="curPage"></param>
        /// <param name="paramValues"></param>
        /// <returns></returns>
        public List <T> ExecuteListObject <T>(string sqlName, int pageSize, int curPage, params object[] paramValues)
        {
            int         i;
            int         recordStart = 0;
            int         recordEnd   = 0;
            List <T>    objs        = new List <T>();
            T           obj;
            IDataReader idr         = null;
            Type        returnClass = null;

            // 如果未指定SQL语句的返回参数类, 将抛出异常
            returnClass = SqlMap.GetSqlReturnClass(sqlName);
            if (returnClass == null)
            {
                throw new Exception(string.Format("未指定SQL语句的返回参数类异常:{0}", sqlName));
            }

            // 计算获取的记录编号范围(当前页)
            CalcRecordRange(pageSize, curPage, ref recordStart, ref recordEnd);

            try
            {
                // 执行查询
                string sql = SqlMap.GetSql(_isql, sqlName, paramValues);
                idr = _db.ExecuteReaderBySql(sql);

                // 获取指定范围的数据
                i = 0;
                while (idr.Read())
                {
                    i++;
                    if (i < recordStart)
                    {
                    }
                    else if (i <= recordEnd)
                    {
                        obj = (T)ReflectUtil.CreateInstance(returnClass);
                        for (int j = 0; j < idr.FieldCount; j++)
                        {
                            ReflectUtil.SetPropertyValue(obj, idr.GetName(j), idr.GetValue(j));
                        }
                        objs.Add(obj);
                    }
                    else //if (i > recordEnd)
                    {
                        break;
                    }
                }
            }
            finally
            {
                if (idr != null)
                {
                    idr.Close();
                }
            }

            return(objs);
        }
示例#2
0
        /// <summary>
        /// 执行查询SQL,返回列表object
        /// 前置条件: 必须是映射的SQL语句,且必须指定SQL语句的返回参数类
        /// </summary>
        /// <param name="sqlName"></param>
        /// <param name="pageSize"></param>
        /// <param name="curPage"></param>
        /// <param name="paramValues"></param>
        /// <returns></returns>
        public PageList <T> ExecutePageListObject <T>(string sqlName, string orderKey, int pageSize, int curPage, params object[] paramValues)
        {
            // 如果未指定SQL语句的返回参数类, 将抛出异常
            Type returnClass = SqlMap.GetSqlReturnClass(sqlName);

            if (returnClass == null)
            {
                throw new Exception(string.Format("未指定SQL语句的返回参数类异常:{0}", sqlName));
            }
            return(ExecutePageListObjectByType <T>(SqlMap.GetSql(_isql, sqlName, paramValues), orderKey, pageSize, curPage, returnClass));
        }