C# MySql分页存储过程的应用
存储过程:
获取范围内的数据
DELIMITER $$ DROP PROCEDURE IF EXISTS `studb`.`GetRecordAsPage` $$ CREATE PROCEDURE `studb`.`GetRecordAsPage` (in tbName varchar(800),in fldName varchar(1000), in strWhere varchar(500),in pageIndex int,in pageSize int,in orderType int,in sortName varchar(50)) BEGIN declare startRow int; declare sqlStr varchar(1000); declare limitTemp varchar(1000); declare orderTemp varchar(1000); set startRow = (pageIndex-1)*pageSize; set sqlStr = CONCAT('SELECT ',fldName,' from ',tbName); set limitTemp = CONCAT(' limit ',startRow,',',pageSize); set orderTemp = CONCAT(' order by ',sortName); if orderType = 0 then set orderTemp = CONCAT(orderTemp,' ASC '); else set orderTemp = CONCAT(orderTemp,' DESC '); end if; set @sqlString = CONCAT(sqlStr,' ',strWhere,orderTemp,limitTemp); prepare sqlstmt from @sqlString; execute sqlstmt; deallocate prepare sqlstmt; END $$ DELIMITER ;
获取条件下的总记录数据
DELIMITER $$ DROP PROCEDURE IF EXISTS `GetRecordCount` $$ -- --CREATE DEFINER=`root`@`localhost` PROCEDURE `GetRecordCount`(in tbName varchar(800),in strWhere varchar(500)) CREATE PROCEDURE `GetRecordCount`(in tbName varchar(800),in strWhere varchar(500)) BEGIN set @strSQL=concat('select count(*) as countStr from ',tbName,strWhere); prepare sqlstmt from @strSQL; execute sqlstmt; deallocate prepare sqlstmt; END $$ DELIMITER ;
测试
建表语句
CREATE TABLE `uinfo` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`iduse` varchar(200) DEFAULT NULL COMMENT '用户编号',
`uname` varchar(200) DEFAULT NULL COMMENT '用户名称',
`email` varchar(200) DEFAULT NULL COMMENT 'Email',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
测试脚本
SELECT
`id`
, `iduse`
, `uname`
, `email`
FROM
`mostimpressive_dscj`.`uinfo`; SET @tbName='uinfo'; -- -- 表名
SET @fldName='iduse,uname,email';-- -- 表的列名
SET @strWhere = ''; -- -- 查询条件
SET @pageIndex=1;-- -- 第几页 传入1就是显示第一页
SET @pageSize=5;-- -- 一页显示几条记录
SET @orderType=0; -- --0是升序 非0是降序
SET @sortName='id'; -- -- 排序字段 CALL Dscj_GetPaged('uinfo','iduse,uname,email','',1,1,1,'id')
-- --SELECT iduse,uname,email from uinfo order by id ASC limit 0,4 -- --传入表名,返回总的记录条数
CALL GetRecordCount('uinfo','')
C#程序
protected void get_data() { string strWhere = " where idmde='" + DropDownList1.SelectedValue + "' and idmke='" + DropDownList2.SelectedValue + "'"; string fldName = "iduse,uname,email,euser,werks"; int pageSize = ; int pageCount = ; int pageCountMod = ; int doCount = ; int pageIndex = System.Convert.ToInt32(pageIndexLab.Text.ToString()); myco = new MySqlConnection(strconn); myco.Open(); mycomm = new MySqlCommand("GetRecordCount", myco); mycomm.CommandType = CommandType.StoredProcedure; MySqlParameter[] para = new MySqlParameter[]{ new MySqlParameter("@tbName",MySqlDbType.VarChar,), new MySqlParameter("@strWhere",MySqlDbType.VarChar,)}; para[].Value = "uinfo"; para[].Value = strWhere; mycomm.Parameters.AddRange(para); MySqlDataReader mydr=mycomm.ExecuteReader(); while(mydr.Read()){ pageCount = System.Convert.ToInt32(mydr["countStr"].ToString()) ; pageCountMod = pageCount % pageSize; pageCount= pageCount/ pageSize; if (pageCount == ) { //pageCount++; } else { if (pageCountMod != ) { pageCount++; } } pageCountLab.Text = pageCount.ToString(); } if (pageIndex == ) { pageIndex++; } mydr.Close(); mycomm.Dispose(); mycomm = new MySqlCommand("GetRecordAsPage", myco); mycomm.CommandType = CommandType.StoredProcedure; para.Initialize(); para = new MySqlParameter[]{ new MySqlParameter("@tbName",MySqlDbType.VarChar,), new MySqlParameter("@fldName",MySqlDbType.VarChar,), new MySqlParameter("@strWhere",MySqlDbType.VarChar,), new MySqlParameter("@pageIndex",MySqlDbType.Int32), new MySqlParameter("@pageSize",MySqlDbType.Int32), new MySqlParameter("@orderType",MySqlDbType.Int16), new MySqlParameter("@sortName",MySqlDbType.VarChar,)}; para[].Value = "uinfo"; para[].Value = fldName; para[].Value = strWhere; para[].Value = pageIndex; para[].Value = pageSize; para[].Value = ; para[].Value = "iduse"; mycomm.Parameters.AddRange(para); MySqlDataAdapter myAdapter = new MySqlDataAdapter(mycomm); DataSet myDataSet = new DataSet(); DataView m_DataView = new DataView(); myAdapter.Fill(myDataSet); m_DataView = myDataSet.Tables[].DefaultView; GridView1.DataSource = m_DataView; GridView1.DataBind(); GridView2.DataSource = m_DataView; GridView2.DataBind(); GridView3.DataSource = m_DataView; GridView3.DataBind(); DropDownpage.Items.Clear(); if (pageCount != ) { doCount = ; while (doCount < pageCount) { doCount++; DropDownpage.Items.Add(new ListItem(doCount.ToString()+"页", doCount.ToString())); } } if (pageIndex == ) { Button_IndexFirst.Enabled = false; Button_IndexPrevious.Enabled = false; Button_IndexNext.Enabled = true; Button_IndexLast.Enabled = true; } else if (pageIndex == pageCount) { Button_IndexFirst.Enabled = true; Button_IndexPrevious.Enabled = true; Button_IndexNext.Enabled = false; Button_IndexLast.Enabled = false; } else { Button_IndexFirst.Enabled = true; Button_IndexPrevious.Enabled = true; Button_IndexNext.Enabled = true; Button_IndexLast.Enabled = true; } if (pageCount == || pageCount == ) { pageIndex = pageCount; Button_IndexFirst.Enabled = false; Button_IndexPrevious.Enabled = false; Button_IndexNext.Enabled = false; Button_IndexLast.Enabled = false; DropDownpage.Enabled = false; } else { DropDownpage.Enabled = true; } pageIndexLab.Text = pageIndex.ToString(); if (pageIndex != ) { DropDownpage.SelectedItem.Selected = false; DropDownpage.Items.FindByValue(pageIndex.ToString()).Selected = true; } myco.Close(); } protected void Button_IndexFirst_Click(object sender, EventArgs e) { pageIndexLab.Text = ""; if (pageCountLab.Text.Equals("")) { pageIndexLab.Text = ""; } get_data(); } protected void Button_IndexPrevious_Click(object sender, EventArgs e) { int pageIndex = System.Convert.ToInt32(pageIndexLab.Text.ToString()); pageIndex--; if (pageIndex < ) { pageIndex = ; } pageIndexLab.Text = pageIndex.ToString(); get_data(); } protected void Button_IndexNext_Click(object sender, EventArgs e) { int pageIndex = System.Convert.ToInt32(pageIndexLab.Text.ToString()); int pageCount = System.Convert.ToInt32(pageCountLab.Text.ToString()); pageIndex++; if (pageIndex > pageCount) { pageIndex = pageCount; } pageIndexLab.Text = pageIndex.ToString(); get_data(); } protected void Button_IndexLast_Click(object sender, EventArgs e) { int pageIndex = System.Convert.ToInt32(pageIndexLab.Text.ToString()); int pageCount = System.Convert.ToInt32(pageCountLab.Text.ToString()); pageIndex = pageCount; pageIndexLab.Text = pageIndex.ToString(); get_data(); } protected void DropDownpage_SelectedIndexChanged(object sender, EventArgs e) { pageIndexLab.Text = DropDownpage.SelectedValue; get_data(); }
C# MySql分页存储过程的应用的更多相关文章
- MYSQL分页存储过程及事务处理
最近给客户做的一小系统是SQLSERVER的数据库,因为特殊原因要切换到MYSQL上去,切换数据库确实让人头疼的,SQLSERVER和MYSQL的存储过程还是有很大差别的,下面是我做切换时转换的MYS ...
- MYSQL分页存储过程及事务处理--转自peace
MYSQL的分页过程,和事务处理的一个测试过程. /* --名称:MYSQL版查询分页存储过程 by peace 2013-8-14 --输入参数:@fields -- 要查询的字段用逗号隔开 --输 ...
- 调用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
随机推荐
- parameter和argument的区别
根据网上一些资料,对parameter和argument的区别,做如下的简单说明.1. parameter是指函数定义中参数,而argument指的是函数调用时的实际参数.2. 简略描述为:param ...
- Nginx 老是抢先捕获了404 我想这个404我Yii项目来处理 要怎么配置?
配置Nginx vhost的时候别指定error_page 它就不会跟Yii抢生意了 or 你弄一个frontendController 然后再里面 加上 $user->loginRequi ...
- VSC 使用Git进行版本控制
Visual Studio Code 使用Git进行版本控制 请确保你安装了最新的VS Code.http://code.visualstudio.com/ 请确保安装了最新版的Git.https:/ ...
- Asp.net 同时下载多个文件
整理自网络 下载思路是首先把多个文件进行压缩,然后再下载压缩成的压缩包 引用文件dll:ICSharpCode.SharpZipLib.dll 1. 合成下载文件夹 Protected Sub btn ...
- mysql安装篇
装了vps,环境又要自己配置.mysql比较难装上. 1.安装MySQL 5.5.x的yum源:rpm -Uvh http://repo.webtatic.com/yum/centos/5/lates ...
- EXT经验--查询EditorGridPanel的tbar的默认配置对象
前言:EXT的API可谓熟悉EXT的葵花宝典,会看API可谓对于配置EXT,学习EXT最重要的基本功,这点相对于学习轻量级的Easyui来说更加明显. 比如下面的一段代码:注:在Ext.grid.Ed ...
- BZOJ1692: [Usaco2007 Dec]队列变换
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 594 Solved: 246[Submit][Sta ...
- action间传多个参数时注意问题
通常我们action之间传参可以有多种形式,举例说明:示例1: <result name="test" type="redirect-action"> ...
- iframe嵌入其他网站,如何自适应高度
终于有一周时间,工作不那么忙了,腾出手来总结下工作过程中学到的知识. 每天遇到新问题,解决新问题,但是却很少有时间去仔细研究下,或者总结下.攒的多了,就得从头捋一遍. 说下iframe自适应高度: 搜 ...
- VMware Workstation 10安装Centos6.4操作步骤说明
1.在网上下载VMware Workstation 10, 百度软件中心助手安装程序高速下载,下载完成后默认是自动启动安装的,而原来的安装程序文件保存在: C:\Users\用户名\Document ...