zoukankan      html  css  js  c++  java
  • MYSQL- 分页存储过程

      工作需要,用到MYSQL的分页功能,在网上找到一个不错的分页存储过程,代码整理了一下!

      存储过程代码

    CREATE PROCEDURE `sp_hj_splitpage`(
     in _pagecurrent int,/*当前页*/
     in _pagesize int,/*每页的记录数*/
     in _ifelse varchar(1000),/*显示字段*/
     in _where varchar(1000),/*条件*/
     in _order varchar(1000)/*排序*/
    )
    COMMENT '分页存储过程'
    BEGIN
     if _pagesize<=1 then 
      set _pagesize=20;
     end if;
     if _pagecurrent < 1 then 
      set _pagecurrent = 1; 
     end if;
     
     set @strsql = concat('select ',_ifelse,' from ',_where,' ',_order,' limit ',_pagecurrent*_pagesize-_pagesize,',',_pagesize); 
     prepare stmtsql from @strsql; 
     execute stmtsql; 
     deallocate prepare stmtsql; 
    
     set @strsqlcount=concat('select count(1) as count from ',_where);/*count(1) 这个字段最好是主键*/
     prepare stmtsqlcount from @strsqlcount; 
     execute stmtsqlcount; 
     deallocate prepare stmtsqlcount; 
    END

      

      调用示例:

    call sp_hj_splitpage(1,3,'*','hj_shangpin_cbj where 1=1','order by id desc');

      C#调用示例:

    #region 分页存储过程
    
            /// <summary>
            /// 分页存储过程
            /// </summary>
            /// <param name="table">表,可以关联:如 A left join B on A.id=B.Aid </param>
            /// <param name="fileds">字段值,获取全部字段用"*"</param>
            /// <param name="order">按什么字段排序</param>
            /// <param name="orderType">排序的方式 有ASC和DESC两种</param>
            /// <param name="pageIndex">页索引</param>
            /// <param name="pageSize">页大小</param>
            /// <param name="strWhere">查询条件,如不查可空 ""</param>
            /// <returns>返回一个DataTable</returns>
            public static DataTable GetSplitPageList(string table, string fileds, string order, string orderType, int pageSize, int pageIndex, string strWhere)
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    string strCmd = string.Format("call sp_hj_splitpage({0},{1},'{2}','{3} where {4}','order by {5} {6}');", pageIndex, pageSize , fileds, table, strWhere, order, orderType);
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand(strCmd, conn);
                    DataTable dt = new DataTable();
                    MySqlDataReader dr = cmd.ExecuteReader();
                    dt.Load(dr);
                    return dt;
                }
            }
    
            /// <summary>
            /// 获得分页总数
            /// </summary>
            /// <param name="table"></param>
            /// <param name="strWhere"></param>
            /// <returns></returns>
            public static int GetSplitPageListCount(string table, string strWhere)
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    string strSql = "select count(*) from " + table + " where " + strWhere;
                    MySqlCommand cmd = new MySqlCommand(strSql, conn);
    
                    return int.Parse(cmd.ExecuteScalar().ToString());
                }
            }
            #endregion

    原文网址:http://blog.csdn.net/jxncwzb/article/details/2883467

  • 相关阅读:
    【luogu】P1772物流运输(最短路+DP)
    【Bzoj】1001狼抓兔子(平面图最小割转对偶图最短路)
    后记
    【Luogu】P2680运输计划(树上差分+二分)
    【Luogu】P2059卡牌游戏(概率DP)
    【Luogu】P2051中国象棋(DP)
    概率与期望学习笔记
    【Luogu】P2894酒店Hotel(线段树)
    21.Mysql Server优化
    20.Mysql锁机制
  • 原文地址:https://www.cnblogs.com/cxeye/p/4634853.html
Copyright © 2011-2022 走看看