• 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

  • 相关阅读:
    4408: [Fjoi 2016]神秘数
    UOJ #35. 后缀排序[后缀数组详细整理]
    POJ 2887 Big String
    搜索过滤grep(win下为findstr)
    解决putty自动断开的问题
    > >> 将错误输出到文件
    环境变量
    端口被占用,查看并杀死占用端口的进程
    查找文件路径find
    【vim使用】
  • 原文地址:https://www.cnblogs.com/cxeye/p/4634853.html
Copyright © 2020-2023  润新知