mysql 分页存储过程 一次返回两个记录集(行的条数,以及行记录),DataReader的Read方法和NextResult方法
DELIMITER $$ USE `netschool`$$ DROP PROCEDURE IF EXISTS `fn_jk_GetCourses`$$ CREATE DEFINER=`root`@`%` PROCEDURE `fn_jk_GetCourses`(IN p_pageIndex INT, IN p_pageSize INT ) BEGIN -- select p_Tid,p_Functionid,p_BeginTime,p_EndTime,p_pageIndex,p_pageSize; -- 定义key字段临时表 DROP TABLE IF EXISTS _temptable_keyid; -- 删除临时表,如果存在 CREATE TEMPORARY TABLE _temptable_keyid ( `CourseId` INT ); -- 构建动态的sql,输出关键字key的id集合 -- 查找条件 SET @SQL =' SELECT `CourseId` from `tb_cs_course` '; -- SET @SQL=CONCAT(@SQL, ' ORDER BY `ExName` DESC '); -- select @SQL; -- 准备id记录插入到临时表 SET @SQL=CONCAT('insert into _temptable_keyid(`CourseId`) ', @SQL); PREPARE stmt FROM @SQL; EXECUTE stmt ; DEALLOCATE PREPARE stmt; -- 下面是输出 SELECT COUNT(*) AS RecordCount FROM _temptable_keyid; -- 计算记录的起点位置 SET @STARTPOINT = IFNULL((p_pageIndex-1)*p_pageSize,0); SET @SQL='select c.`CourseId`,`CourseName` AS `Name`,`GetSubject`(`SubjectId`) AS `Subject`,`GetGradeName`(`GradeId`) AS `Grade`, `GetTeacherName`(`TeacherId`) AS Teacher,`GetKnowledgeNameByCourseid`(c.`CourseId`) AS Knowledge,`VideoCount`,`ExName` AS Image,`PicUrl` AS Poster from `tb_cs_course` c inner join _temptable_keyid t on c.`CourseId`=t.`CourseId` '; SET @SQL=CONCAT(@SQL, ' limit ',@STARTPOINT,' , ',p_pageSize); PREPARE stmt FROM @SQL; EXECUTE stmt ; DEALLOCATE PREPARE stmt; DROP TABLE _temptable_keyid; -- 给出实际查询的表 -- 结束 END$$ DELIMITER ;
会返回两个结果,一个是 RecordCount 行数 ,一个是真正的记录
那么怎么去读取这2个值呢?
public List<MGetXiaoWuHistory> GetMessageHistoryByPage(int functionId, string tId, string beginTime,string endTime, int pg, int pagesize,string key, ref int recordcount) { IDataReader rdr = DataProvider.Instance().GetMessageHistoryByPage(functionId, tId, beginTime, endTime, pg, key,pagesize); List<MGetXiaoWuHistory> objArray = null; if (rdr.Read()) { recordcount = Convert.ToInt32(rdr["RecordCount"]); } if (rdr.NextResult()) { objArray = CBO.FillCollection<MGetXiaoWuHistory>(rdr); } return objArray; }