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

  • 相关阅读:
    MySQL第五天
    phprpc
    最近数据库不怎么太平
    dumpfile参数,linux和windows下稍有不同
    impdp 时无反应
    修改IP后,重新config EM
    ORA00054: resource busy and acquire with NOWAIT specified
    测试修改DBID手记
    如何关闭任务计划
    文件夹打开变成寻找框如何解决
  • 原文地址:https://www.cnblogs.com/cxeye/p/4634853.html
Copyright © 2011-2022 走看看