1.高校分页( C#代码+sqlserver)

1.数据库(分页)初入====================

 

2.分页查询,查询前10条(不管id有没有连续,都会显示10条数据)

    select top 10 * from Students

3.分页查询第三页每页显示10条数据 (条件是满足id不在前20条中)只要数据够:就会显示第三页:10条数据

  select top 10 * from Students
where Id not in (
select top 20 id from Students

第三页数据,运行结果示例如下:

 4.分页查询,排序:查询第三页数据(根据条件name查询:1.asc升序,2.desc降序) 

sql :select top 10 * from Studentswhere Id not in (select top 20 id from Students order by Name asc)

order by Name asc

运行结果示例如下:

 5.分页存储过程=======================

 

alter proc pageData ---存储过程数据表---
@pageindex int, --页码--
@pagesize int=10,--页容量
@isDel bit=0, --是否删除
@rowCount float output, --输出总行数
@pageCount float output--输出 总页数
as
begin
//@rowCount求总行数
select @rowCount=COUNT(cid)from Classes where CIsDel=@isDel
//@pageCount页数
select @pageCount=CEILING(@rowCount/@pagesize)--使用天花板函数,将小数的数值,加1去小数
select *from(select ROW_NUMBER()over(order by cid) as rownum,*from Classes where CIsDel=@isDel )
as temp where temp.rownum>(@pageindex-1)*@pagesize and temp.rownum<=@pageindex*@pagesize
end
declare @rc int,@pc int
execute pageData 3,10,0,@rc output,@pc output ---@rc总行,@pc总页数
select @rc,@pc

 

 二.分页2

 

.

DAL层:

6.1.C#类calss.cs(数据层中何调用)

   getpageListByprod执行存储过程方法,Term4Getpageddata(存储过程表名字) 下面要把isdel对象    加进去。

Table2List(dt);是哪里来的啊;小弟给你介绍下:

 LoadEntityData(model,dr);这个方法又是从哪里来的了,小弟为你介绍:
  就是先判断下行里是否有这个列,不允许为空。 然后取出来赋给实体对象。

BLL层:

6.2  calss.cs(业务层BLL)

 

DBHelper层:

HelperSQL中,下面获取存储过程返回输出参数顺序写反了,要调换下位置

 

10.高校分页=====================



SQLServer 存储过程分页

create Procedure prc_GetRecordForPaging
   @tableName varchar(255),--表名
   @fieldName varchar(255),--字段名称
   @pageSize int=10,       --单页显示大小
   @pageIndex int=1,       --页码
   @orderType bit=0,       --设置排序类型,非0则是降序
   @strCondition varchar(2000)='' --查询条件(注意,不要添加where语句)
 as
   declare @strSQL varchar(6000) --主执行语句
   declare @strTemp varchar(1000) --临时执行语句
   declare @strOrder varchar(500) --排序语句
   if @orderType != 0 --降序
      begin
        set @strTemp='< (select min' 
        set @strOrder=' order by ['+@fieldName+'] desc' --排序语句
      end
   else    -- 升序
      begin
        set @strTemp ='> (select Max'
        set @strOrder =' order by ['+@fieldName+'] asc'
      end
   set @strSQL ='select top'+str(@pageSize)+' * from '
               + @tableName+'] where ['+ @fieldName+ ']'
               + @strTemp+ '(['+ @fieldName+ ']) from ( select top'+
               + str(@pageSize*(@pageIndex-1))+'['
               + @fieldName +'] from ['+ @tableName+']'
               + @strOrder + ') as tableTemp)'
               + @strOrder
 
   if @strCondition != ''
      set @strSQL ='select top'+str(@pageSize)+' * from '
               + @tableName+'] where ['+ @fieldName+ ']'
               + @strTemp+ '(['+ @fieldName+ ']) from ( select top'+

                + str(@pageSize*(@pageIndex-1))+'['
               + @fieldName +'] from ['+ @tableName+'] where '
               + @strCondition + '' + @strOrder + ') as tableTemp) and'
               + @strCondition + '' + @strOrder
   if @pageIndex = 1
       begin
         set @strTemp =''
           if @strCondition !=''
              set @strTemp =' where ('+ @strCondition + ')'
           
           set @strSQL ='select top'+ str(@pageSize)+ ' * from ['
                       + @tableName+ ']'+ @strTemp+ '' + @strOrder
       end
   Execute(@strSQL)
 Go

 

分页存储过程二:

create procedure pro_GetRecordForPaging
     @tableName varchar(200),--表名
     @pageSize int, --每页显示记录数
     @pageIndex int, --当前页码
     @colName varchar(30) --排序的字段名
 as
   begin
     declare @strSQL varchar(300)
     if @pageIndex = 1
       set @strSQL ='select top ['+cast(@pageSize as varchar(20)) + ']'
                    + ' * from ['+ @tableName + ']'
                    + ' order by ['+ @colName + ']'
     else
       set @strSQL='select top ['+cast(@pageSize as varchar(20)) + ']'
                   + ' * from ['+ @tableName + ']'
                   + ' where ['+ @colName + ']'
                   + ' not in ( select top ['
                   + cast((@pageIndex-1)*pageSize as varchar(20)) + ']'
                   + @colName + ' from ['+ @tableName + ']'
                   + 'order by ['+ @colName +'])'
                   + 'order by ['+ @colName +']'
     --print
     Execute(@strSQL)
   end
 go

 

 

posted @ 2016-07-14 23:48  狼牙者.net  阅读(429)  评论(0)    收藏  举报