分页公用方法 有oracle,mysql

 /// <summary>
        ///  根据查询条件获取指定表中的行数
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="pageSize"></param>
        /// <param name="whereList"></param>
        /// <param name="RowCount"></param>
        /// <returns></returns>
        public static int GetRowCount(int pageSize, out int itemCount, string tableName, System.Collections.Generic.Dictionary<string, string> whereList)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select count(1) from {0} where 1=1 ", tableName);
            if (whereList != null && whereList.Count > 0)
            {
                foreach (System.Collections.Generic.KeyValuePair<string, string> w in whereList)
                {
                    if (w.Key == "SQL")
                    {
                        strSql.Append(w.Value);
                    }
                    else if (w.Key == "ORDER" || w.Key == "SORT")
                        continue;
                    else
                    {
                        strSql.AppendFormat(" and {0}='{1}'", w.Key, w.Value);
                    }
                }
            }
            itemCount = int.Parse(DbHelperMySQL.GetSingle(strSql.ToString()).ToString());
            if (pageSize == 0 || itemCount == 0) return 0;
            if (itemCount % pageSize == 0)
                return itemCount / pageSize;
            else
                return itemCount / pageSize + 1;
        }
        /// <summary>
        /// Oracle分页方法
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示行数</param>
        /// <param name="itemCount">返回总行数</param>
        /// <param name="tableName">要获取的表</param>
        /// <param name="whereList">Dictionary查询条,key,value键值形式(排序字段key: ORDER,升序降序 key:SORT,特殊查询条件key:SQL)</param>
        /// <returns></returns>
        public static DataSet GetOraclePageList(int currentPage, int pageSize, out int itemCount, string tableName, System.Collections.Generic.Dictionary<string, string> whereList)
        {
            int rowCount = GetRowCount(SQLDBType.ORACLE, pageSize, out itemCount, tableName, whereList);
            if (currentPage <= 1) currentPage = 1;//最小页
            if (currentPage >= rowCount) currentPage = rowCount;//最大页

            StringBuilder strSql = new StringBuilder();
            #region 公用分页查询语句
            strSql.Append("select * from ( ");
            strSql.Append(" select a.*,rownum as rn from ( ");
            strSql.AppendFormat("select *  from {0} t where 1=1 ", tableName);
            if (whereList != null && whereList.Count > 0)
            {
                foreach (System.Collections.Generic.KeyValuePair<string, string> w in whereList)
                {
                    if (w.Key == "SQL")
                    {
                        strSql.Append(w.Value);
                    }
                    else if (w.Key == "ORDER" || w.Key == "SORT")
                        continue;
                    else
                    {
                        strSql.AppendFormat(" and {0}='{1}'", w.Key, w.Value);
                    }
                }

            }
            if (whereList.ContainsKey("ORDER"))
            {
                strSql.AppendFormat(" order by {0} ", whereList["ORDER"]);
                if (whereList.ContainsKey("SORT"))
                    strSql.Append(whereList["SORT"]);
            }
            strSql.Append(")a ");
            strSql.Append(")b");
            strSql.AppendFormat(" where b.rn between {0} and {1}", (currentPage - 1) * pageSize + 1, (currentPage - 1) * pageSize + pageSize);
            #endregion
            return DBUtility.DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// mysql分页方法
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示行数</param>
        /// <param name="itemCount">返回总行数</param>
        /// <param name="tableName">要获取的表</param>
        /// <param name="whereList">Dictionary查询条,key,value键值形式(排序字段key: ORDER,升序降序 key:SORT,特殊查询条件key:SQL)</param>
        /// <returns></returns>
        public static DataSet GetMySqlPageList(int currentPage, int pageSize, out int itemCount, string tableName, System.Collections.Generic.Dictionary<string, string> whereList)
        {
            int rowCount = GetRowCount(pageSize, out itemCount, tableName, whereList);
            if (currentPage <= 1) currentPage = 1;//最小页
            if (currentPage >= rowCount && rowCount>0) currentPage = rowCount;//最大页

            StringBuilder strSql = new StringBuilder();
            #region 公用分页查询语句
            strSql.AppendFormat("select *  from {0}  where 1=1 ", tableName);
            if (whereList != null && whereList.Count > 0)
            {
                foreach (System.Collections.Generic.KeyValuePair<string, string> w in whereList)
                {
                    if (w.Key == "SQL")
                    {
                        strSql.Append(w.Value);
                    }
                    else if (w.Key == "ORDER" || w.Key == "SORT")
                        continue;
                    else
                    {
                        strSql.AppendFormat(" and {0}='{1}'", w.Key, w.Value);
                    }
                }
                if (whereList.ContainsKey("ORDER"))
                {
                    strSql.AppendFormat(" order by {0} ", whereList["ORDER"]);
                    if (whereList.ContainsKey("SORT"))
                        strSql.Append(whereList["SORT"]);
                }

            }
            
            strSql.AppendFormat(" LIMIT {0},{1}", (currentPage - 1) * pageSize, pageSize);
            #endregion
            return DbHelperMySQL.Query(strSql.ToString());
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值