以學生資訊表為例
一、建立分頁儲存過程
create proc proc_findstudentsplitpage
--------------------------宣告引數----------------
@pagesize int, --每頁記錄數 輸入引數
@curpage int, --當前是第幾頁 輸入引數
@totalpage int out, --總頁數 輸出引數
@totalcount int out--總記錄數 輸出引數
as-------------------定義變數-------------------------------------------
declare @strsql varchar(200) --要執行的sql語句
--計算總頁數和總記錄數
select @totalcount = count(*) from student
if (@totalcount % @pagesize = 0) --------總記錄數正好按每頁記錄的個數年分均不多不少
set @totalpage = @totalcount / @pagesize
else --------------------記錄數分不均,不夠一頁,或者最後一頁不滿
set @totalpage = @totalcount / @pagesize + 1
--構造分頁查詢語句 以主鍵為標準
set @strsql = 'select top ' + cast(@pagesize as varchar)
set @strsql = @strsql + ' * from student where stuno not in'
set @strsql = @strsql + ' (select top '+ cast(@pagesize * (@curpage-1) as varchar)+' stuno from student)'
--列印執行sql語句
print @strsql
exec(@strsql)--執行,注意()
print '總頁數:' + cast(@totalpage as varchar)
print '總記錄數:' + cast(@totalcount as varchar)
二、在工程中應用
1、建立實體類
using system;
using system.collections.generic;
using system.text;
namespace studentaccesslib
set
}public datetime stuage
set
}public string stuname
set
}public string stuno
set }}
}2、資料訪問物件
using system;
using system.collections.generic;
using system.collections;
using system.data;
using system.data.sqlclient;
namespace studentaccesslib
dr.close();
comm.dispose();
comm.connection.close();
totalpage = int32.parse(comm.parameters[2].value.tostring());
totalcount = int32.parse(comm.parameters[3].value.tostring());
}catch (exception e)
//返回stulist集合
return stulist;}}
}3、在頁面中新增datalist控制項,頁面檔案如下:
4、cs後台**
using system;
using system.data;
using system.configuration;
using system.web;
using system.io;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class _default : system.web.ui.page
}private void bindstudent()
protected void btnfirst_click(object sender, eventargs e)
protected void btnpreced_click(object sender, eventargs e)
}protected void btnnext_click(object sender, eventargs e)
}protected void btnlast_click(object sender, eventargs e)
儲存過程實現模糊查詢分頁
if exists select 1 from sysobjects where name getnewscount and type p drop proc getnewscount gocreate procedure getnewscount title nvarchar asselect c...
分頁查詢儲存過程
分頁查詢儲存過程 object storedprocedure dbo pagination3 script date 2019 1 11 9 02 01 set ansi nulls on goset quoted identifier on goalter procedure dbo pagin...
儲存過程實現分頁
一 create procedure dbo basic pagination2000 tblname varchar 255 表名 fidlelist varchar 2000 要查詢字段 fldname varchar 255 排序字段 pagesize int,頁尺寸 pageindex in...