/// <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); }
/// <summary> /// 执行查询SQL,返回列表object /// </summary> /// <param name="sql"></param> /// <param name="pageSize"></param> /// <param name="curPage"></param> /// <param name="returnClass">不能为null</param> /// <returns></returns> public List <T> ExecuteListObjectByType <T>(string sql, int pageSize, int curPage, Type returnClass) { int i; int recordStart = 0; int recordEnd = 0; List <T> objs = new List <T>(); T obj; IDataReader idr = null; // 计算获取的记录编号范围(当前页) CalcRecordRange(pageSize, curPage, ref recordStart, ref recordEnd); try { // 执行查询 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 // (i > reocrdEnd) { break; } } } finally { if (idr != null) { idr.Close(); } } return(objs); }
/// <summary> /// 执行查询SQL,返回列表object /// </summary> /// <param name="sql"></param> /// <param name="pageSize"></param> /// <param name="curPage"></param> /// <param name="returnClass">不能为null</param> /// <returns></returns> public PageList <T> ExecutePageListObjectByType <T>(string sql, string orderKey, int pageSize, int curPage, Type returnClass) { PageList <T> objs = new PageList <T>(); T obj; IDataReader idr = null; // 计算获取的记录编号范围(当前页) objs.SetPage(pageSize, curPage); try { // 获取总记录数 if (pageSize != -1) { objs.TotalCount = ParamUtil.getint(_db.ExecuteScalarBySql(_isql.CountSql(sql))); } string pageSql = _isql.PageSql(sql, orderKey, objs.StartRecord, objs.EndRecord); // 执行查询 idr = _db.ExecuteReaderBySql(pageSql); // 获取指定范围的数据 while (idr.Read()) { obj = (T)ReflectUtil.CreateInstance(returnClass); // 第一列为COL_ROWNUM(记录号),不设置属性值 for (int i = 1; i < idr.FieldCount; i++) { ReflectUtil.SetPropertyValue(obj, idr.GetName(i), idr.GetValue(i)); } objs.Add(obj); } // 如果未分页,则设置总数为当前页记录数 if (pageSize == -1) { objs.TotalCount = objs.CurPageCount; } } finally { if (idr != null) { idr.Close(); } } return(objs); }