现在我有个分页的存储过程,并且查询数据的时候已经对数据进行了相关的处理,我在用datagird进行显示的时候,请问,存储过程计算的总页数等相关参数是怎么取出来的??
解决方案 »
- jquery在asp.net ajax 使用updatepanel提交后失效
- 自定控件GetPostBackEventReference问题
- 子窗口與父窗口怎么
- 请教,下面这段代码为什么出现 CS201错误呢?网速慢,搜索了麻烦啊,恳请大家指教
- 没有可访问的“ToString”接受此数目的参数,因此重载决策失败。
- 原来有个收银台软件,现在要做个商城,他们说要把收银台整合到商城里,怎么做,高手们给点意见????
- 请问如何把一个button的触发事件限制在一段时间内?
- 怎样把一个table的四个角做成平滑的曲线?
- 如何比较两个对象的类型一样啊
- 客户耍花招不付款拖死创业公司,怎么办
- |M| 开始想用DIV代替Table,想问一下有什么要注意的
- 用户控件引用后出现的问题
建立表: CREATE TABLE [TestTable] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[FirstName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[LastName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[Country] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[Note] [nvarchar] (2000) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO 插入数据:(2万条,用更多的数据测试会明显一些)
SET IDENTITY_INSERT TestTable ON declare @i int
set @i=1
while @i<=20000
begin
insert into TestTable([id], FirstName, LastName, Country,Note) values(@i, 'FirstName_XXX','LastName_XXX','Country_XXX','Note_XXX')
set @i=@i+1
end SET IDENTITY_INSERT TestTable OFF ------------------------------------- 分页方案一:(利用Not In和SELECT TOP分页)
语句形式:
SELECT TOP 10 *
FROM TestTable
WHERE (ID NOT IN
(SELECT TOP 20 id
FROM TestTable
ORDER BY id))
ORDER BY ID
SELECT TOP 页大小 *
FROM TestTable
WHERE (ID NOT IN
(SELECT TOP 页大小*页数 id
FROM 表
ORDER BY id))
ORDER BY ID ------------------------------------- 分页方案二:(利用ID大于多少和SELECT TOP分页)
语句形式:
SELECT TOP 10 *
FROM TestTable
WHERE (ID >
(SELECT MAX(id)
FROM (SELECT TOP 20 id
FROM TestTable
ORDER BY id) AS T))
ORDER BY ID
SELECT TOP 页大小 *
FROM TestTable
WHERE (ID >
(SELECT MAX(id)
FROM (SELECT TOP 页大小*页数 id
FROM 表
ORDER BY id) AS T))
ORDER BY ID
------------------------------------- 分页方案三:(利用SQL的游标存储过程分页)
create procedure XiaoZhengGe
@sqlstr nvarchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int --每页行数
as
set nocount on
declare @P1 int, --P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off 其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。
建议优化的时候,加上主键和索引,查询效率会提高。 通过SQL 查询分析器,显示比较:我的结论是:
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句
分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接SQL语句
分页方案三:(利用SQL的游标存储过程分页) 效率最差,但是最为通用 在实际情况中,要具体分析。
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE procedure [dbo].[sp_PageChange]
@PageIndex INT,
@PageSize INT,
@RecordCount INT OUT,
@PageCount INT OUT,
@strGetFields nvarchar(1000),
@tableName nvarchar(500) ,
@ID nvarchar(100),
@strWhere nvarchar(1000) ='',
@sortName nvarchar(50) =' asc ' ,
@orderName nvarchar(100) AS
declare @countSelect nvarchar(2000)
--设置统计查询语句
if len(@strWhere) =0
--如果没有查询条件
begin
set @countSelect=N'SELECT @CountRecord = COUNT(*) FROM '+@tableName
end
else
--否则
begin
set @countSelect=N'SELECT @CountRecord = COUNT(*) FROM '+@tableName+' where '+@strWhere
end
--执行并返回总数
exec sp_executesql @countSelect,N'@CountRecord int output',@RecordCount output
SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize) SET NOCOUNT ONDECLARE @SQLSTR NVARCHAR(3000)
--实际总共的页码小于当前页码 或者 最大页码
if @PageCount>=0
--如果分页后页数大于0
begin
if @PageCount<=@PageIndex and @PageCount>0 --如果实际总共的页数小于datagrid索引的页数
--or @PageCount=1
begin
--设置为最后一页
set @PageIndex=@PageCount-1
end
else if @PageCount<=@PageIndex and @PageCount=0
begin
set @PageIndex=0;
end
end
--如果用普通的sql而不使用存储过程调用
declare @ID_temp varchar(100)
set @ID_temp='cast('+@ID+' as nvarchar(100)) '
declare @returnValue nvarchar(100)
set @returnValue=','''+cast(@RecordCount as nvarchar(100)) +'|'+cast(@PageCount as nvarchar(100))+'|'+'''+'+@ID_temp+' as [returnValue] '
--如果用普通的sql而不使用存储过程调用
IF @PageIndex = 0 OR @PageCount <= 1 --如果为第一页
begin
if len(@strWhere) =0
begin
SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+@strGetFields+@returnValue+' FROM '+@tableName+' ORDER BY '+@orderName+@sortName
end
else
begin
SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+@strGetFields+@returnValue+' FROM '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName
end
end
ELSE IF @PageIndex = @PageCount - 1 --如果为最后一页
begin
if len(@strWhere) =0
begin
SET @SQLSTR =N' SELECT '+@strGetFields+@returnValue+' FROM '+@tableName+' where '+@ID+' not in ( SELECT TOP '+STR(/*@RecordCount - */@PageSize * @PageIndex )+@ID+' FROM '+@tableName+' ORDER BY '+@orderName+@sortName+' ) ORDER BY '+@orderName+@sortName
end
else
begin
SET @SQLSTR =N' SELECT '+@strGetFields+@returnValue+' FROM '+@tableName+' where '+@ID+' not in ( SELECT TOP '+STR(/*@RecordCount - */ @PageSize * @PageIndex )+@ID+' FROM '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' ) and '+@strWhere+' ORDER BY '+@orderName+@sortName
end
end
ELSE --否则执行
begin
if len(@strWhere) =0
begin
SET @SQLSTR =N' SELECT TOP '+STR( @PageSize )+@strGetFields+@returnValue+' FROM '+@tableName+' where '+@ID+' not in ( SELECT TOP '+STR( /*@RecordCount - */@PageSize * @PageIndex )+' '+@ID+' FROM '+@tableName+' ORDER BY '+@orderName+@sortName+' ) ORDER BY '+@orderName+@sortName
end
else
begin
SET @SQLSTR =N' SELECT TOP '+STR( @PageSize )+@strGetFields+@returnValue+' FROM '+@tableName+' where '+@ID+' not in (SELECT TOP '+STR(/*@RecordCount - */ @PageSize * @PageIndex )+@ID+' FROM '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' )and '+@strWhere+'ORDER BY '+@orderName+@sortName
end
end
EXEC (@SQLSTR)set nocount offGO用这个存储过程试试看,它的第三个参数就是总页数
我不知道你的存储过程是怎么样的?你可以在存储过程中增加一个输出参数
@PageCount int OutPut
在存储过程中给输出参数赋值,在调用存储过程后,就可以在程序中取出
这个参数的值了
然后再程序中获取这两个表就可以了。这样的话,我把存储过程的结果放进DataSet的两个表,会不会明显影响性能?