首页 > 代码库 > 使用存储过程分页
使用存储过程分页
USE [OA]GO/****** Object: StoredProcedure [dbo].[spGetByPage] Script Date: 11/05/2014 13:01:37 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[spGetByPage](@tblName varchar(255), -- 表名@strGetFields varchar(1000) = ‘*‘, -- 需要返回的列@fldName varchar(255)=‘ID‘, -- 排序的字段名@PageSize int = 5, -- 页尺寸@PageIndex int = 1, -- 页码@doCount bit = 5, -- 返回记录总数, 非 0 值则返回@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序@strWhere varchar(1500)=‘1=1‘ -- 查询条件 (注意: 不要加 where))ASdeclare @strSQL varchar(5000) -- 主语句declare @strTmp varchar(110) -- 临时变量declare @strOrder varchar(400) -- 排序类型 if @doCount != 0 begin if @strWhere !=‘‘ set @strSQL = ‘select count(*) as Total from ‘ + @tblName + ‘ where ‘+@strWhere else set @strSQL = ‘select count(*) as Total from ‘ + @tblNameend --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况elsebegin if @OrderType != 0begin set @strTmp = ‘<(select min‘set @strOrder = ‘ order by ‘ + @fldName +‘ desc‘--如果@OrderType不是0,就执行降序,这句很重要!endelsebegin set @strTmp = ‘>(select max‘ set @strOrder = ‘ order by ‘ + @fldName +‘ asc‘end if @PageIndex = 1begin if @strWhere != ‘‘ set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘ + @tblName + ‘ where ‘ + @strWhere + ‘ ‘ + @strOrder else set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘+ @tblName + ‘ ‘+ @strOrder--如果是第一页就执行以上代码,这样会加快执行速度endelsebegin--以下代码赋予了@strSQL以真正执行的SQL代码set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘ + @tblName + ‘ where ‘ + @fldName + ‘‘ + @strTmp + ‘(‘+ @fldName + ‘) from (select top ‘ + str((@PageIndex-1)*@PageSize) + ‘ ‘+ @fldName + ‘ from ‘ + @tblName + ‘‘ + @strOrder + ‘) as tblTmp)‘+ @strOrder if @strWhere != ‘‘ set @strSQL = ‘select top ‘ + str(@PageSize) +‘ ‘+@strGetFields+ ‘ from ‘ + @tblName + ‘ where ‘ + @fldName + ‘‘ + @strTmp + ‘(‘ + @fldName + ‘) from (select top ‘ + str((@PageIndex-1)*@PageSize) + ‘ ‘ + @fldName + ‘ from ‘ + @tblName + ‘ where ‘ + @strWhere + ‘ ‘ + @strOrder + ‘) as tblTmp) and ‘ + @strWhere + ‘ ‘ + @strOrderendend exec (@strSQL)
C#调用
/// <summary> /// get balanceServers by page /// </summary> /// <param name="pageSize"></param> /// <param name="pageIndex"></param> /// <param name="totalCount"></param> /// <returns></returns> public List<BalanceServers> GetBalanceServerByPage(int pageSize, int pageIndex, out int totalCount) { List<BalanceServers> balanceList = null; totalCount = 0; string sp = "spGetByPage"; SqlParameter[] spPage ={ new SqlParameter("@tblName","BalanceServers"), new SqlParameter("@strGetFields","*"), new SqlParameter("@fldName","Guid"), new SqlParameter("@PageSize",pageSize), new SqlParameter("@PageIndex",pageIndex), new SqlParameter("@doCount",0), new SqlParameter("@OrderType",0) };try { using (SqlDataReader reader = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, sp, spPage)) { balanceList = CBO.FillCollection<BalanceServers>(reader); } } catch (Exception ex) { log.Info("Error:" + ex); } return balanceList; }
public static List<T> FillCollection<T>(SqlDataReader reader) where T : class, new() { List<T> tlist = new List<T>() ; while (reader.Read()) { if (tlist == null) tlist = new List<T>(); T t = new T(); foreach (PropertyInfo p in t.GetType().GetProperties()) { try { object coloumnValue =http://www.mamicode.com/ reader[p.Name]; Type objDataType = coloumnValue.GetType(); if (coloumnValue =http://www.mamicode.com/= null || coloumnValue =http://www.mamicode.com/= DBNull.Value) { p.SetValue(t, Null.SetNull(p), null); } else { p.SetValue(t, coloumnValue, null); } } catch (Exception) { } } tlist.Add(t); } return tlist; }
使用存储过程分页
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。