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- 分页存储过程的更多相关文章
- MYSQL分页存储过程及事务处理
最近给客户做的一小系统是SQLSERVER的数据库,因为特殊原因要切换到MYSQL上去,切换数据库确实让人头疼的,SQLSERVER和MYSQL的存储过程还是有很大差别的,下面是我做切换时转换的MYS ...
- MYSQL分页存储过程及事务处理--转自peace
MYSQL的分页过程,和事务处理的一个测试过程. /* --名称:MYSQL版查询分页存储过程 by peace 2013-8-14 --输入参数:@fields -- 要查询的字段用逗号隔开 --输 ...
- C# MySql分页存储过程的应用
存储过程: 获取范围内的数据 DELIMITER $$ DROP PROCEDURE IF EXISTS `studb`.`GetRecordAsPage` $$ ),), ),)) BEGIN de ...
- 调用MySql 分页存储过程带有输入输出参数
Create PROCEDURE getuser ( IN pageIndex INT, IN pageSize INT, OUT count INT ) BEGIN )*pageSize; sele ...
- mysql 分页存储过程 一次返回两个记录集(行的条数,以及行记录),DataReader的Read方法和NextResult方法
DELIMITER $$ USE `netschool`$$ DROP PROCEDURE IF EXISTS `fn_jk_GetCourses`$$ CREATE DEFINER=`root`@` ...
- mysql分页存储过程一步一步实现
1. CREATE DEFINER=`root`@`localhost` PROCEDURE `P_HoverTreePages`( ), ) , ), ), ), IN `SortType` INT ...
- MySQL分页存储过程
CREATE PROCEDURE ProcPage(in tableName varchar(20),#表名 in showField varchar(100),#要显示的列名 in whereT ...
- MySql通用分页存储过程
MySql通用分页存储过程 1MySql通用分页存储过程 2 3过程参数 4p_cloumns varchar(500),p_tables varchar(100),p_where varchar(4 ...
- MYSQL版查询分页存储过程
/*--名称:MYSQL版查询分页存储过程 --输入参数:@fields -- 要查询的字段用逗号隔开--输入参数:@tables -- 要查询的表--输入参数:@where -- 查询条件--输入参 ...
- mysql通用分页存储过程遇到的问题(转载)
mysql通用分页存储过程遇到的问题(转载) http://www.cnblogs.com/daoxuebao/archive/2015/02/09/4281980.html
随机推荐
- CodePage------Encoding 类支持的编码以及与这些编码关联的代码页(CodePage)
Encoding 类 .NET Framework 4 表示字符编码. 继承层次结构 System.Object System.Text.Encoding System.Text.ASCII ...
- VS2005工程由Pocket PC 2003 SDK转为WINCE6.0 SDK的问题
把VS2005工程有采用的Pocket PC 2003 SDK改为WINCE6.0 SDK,具体操作见链接 http://blog.csdn.net/loongembedded/article/det ...
- 254 shades of grey
254 shades of grey Description: Why would we want to stop to only 50 shades of grey? Let's see to ho ...
- 1628. White Streaks(STL)
1628 题意不太好理解 求横黑条 和竖黑条共有多少个 注意1*1的情况 如果横向纵向都是1*1 算为一个 否则不算 用了下vector 枚举找下 #include <iostream> ...
- createSQLQuery与createQuery的区别
本文原址 : http://stta04.javaeye.com/blog/377633hibernate 中createQuery与createSQLQuery 昨晚帮同事看代码到凌晨2点多,今早6 ...
- Innodb物理存储结构系列1
本篇先介绍 下Innodb表空间,文件相关的内存数据结构. 1. 数据结构 Innodb的tablespace和文件的关系,是一对多的关系,先来看三个结构体 1. fil_system_struct: ...
- Gentoo安装
Gentoo Linux安装详解--根据官方WiKi整理 时间:2014-06-26 06:37:54 阅读:549 评论:0 收藏:0 [点我收藏+] 标签: ...
- 【MooTools】
MooTools a compact javascript frameworkhttp://mootools.net/docs/core 30天学会 MooTools 教学(1): 认识MooTool ...
- Java [Leetcode 40]Combination Sum II
题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...
- Beginning Android 4 Programming Book学习
Chapter 3 EditText不自动获取焦点,自动获取焦点但不显示软键盘 Page 122-123 只有定义了android:id属性的控件在屏幕翻转后状态才会被持久化 Page 133 C ...