100 求分求分页存储过程及C#调用示例
分求分页存储过程及C#调用示例 最好可以详细一点的,分页存储过程可以和gridview绑定吗? 谢谢
------解决方案--------------------分页方法很多,我用的是增加临时表的方式.以NorthWind为例:
CREATE PROCEDURE dbo.GetCustomerDataList
(
@pageCount int,
@lineCount int
)
AS
SET NOCOUNT ON
DECLARE @initCount int
SET @initCount = @lineCount * (@pageCount - 1)
if(@pageCount = 1 and @initCount = 0)
set @initCount = 1
SET ROWCOUNT @initCount
SELECT
CustomerID
INTO
#WORK
FROM
Customers
ORDER BY
CustomerID
SET ROWCOUNT @lineCount
SELECT
CustomerID,
CompanyName,
ContactName,
ContactTitle,
Address,
City,
Region,
PostalCode,
Country,
Phone,
Fax
FROM
Customers
WHERE
(@pageCount <> 1
AND NOT EXISTS(
SELECT * FROM #WORK
WHERE
#WORK.CustomerID = Customers.CustomerID
)
)
OR
(@pageCount = 1 AND 1 = 1)
ORDER BY
CustomerID
DROP TABLE #WORK
RETURN
GO
------解决方案--------------------给你个简单有效的分页存储过程吧:
ALTER PROCEDURE dbo.GetData
@Size INT = 50, --每页记录数
@Index INT = 1, --页索引
@SQL NVARCHAR(4000), --任何查询语句
@RowCount int = 0 OUTPUT -- 查询结果的总记录数
AS
DECLARE @i INT
SET NOCOUNT ON
SET @Index=(@Index-1)*@Size+1
EXEC SP_CURSOROPEN @i OUTPUT,@SQL ,@scrollopt=1,@ccopt=1, @rowcount= @Count OUTPUT
EXEC SP_CURSORFETCH @i,16,@Index,@Size
EXEC SP_CURSORCLOSE @i
SET NOCOUNT OFF
------解决方案--------------------分页存储过程
http://blog.csdn.net/hertcloud/archive/2006/04.aspx
------解决方案--------------------create proc sp_PublicTurnPage(
@TBName nvarchar(2000)= ' ',--表名,如 pinyin
@PageSizeint=10,--每页的记录数,默认为 10
@CurPageint=1,--表示当前页 1
@KeyFieldnvarchar(100)= 'ID ',--关键字段名,默认为 ID,该字段要求是表中的索引 或 无重复和不为空的字段
@KeyAscDescnvarchar(4)= 'ASC ',--关键字的升、降序,默认为升序 ASC , 降序为 DESC
@Fieldsnvarchar(2000)= '* ',--所选择的列名,默认为全选
@Conditionnvarchar(2000)= ' ',--where 条件,默认为空
@Ordernvarchar(200)= ' '--排序条件,默认为空
)as
if @TBName = ' '
begin
raiserror( '请指定表名! ',11,1)
return
end
if @PageSize <=0 or @CurPage <0
begin
raiserror( '当前页数和每页的记录数都必须大于零! ',11,1)
return
end
if @KeyAscDesc = 'DESC '
set @KeyAscDesc = ' < '
else
set @KeyAscDesc = '> '
if @Condition <> ' '
set @Condition = ' where ' + @Condition
declare @SQL nvarchar(2000)
set @SQL = ' '
if @CurPage = 1
set @SQL = @SQL + 'SELECT Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + ' ' + @Order
else
begin
declare @iTopNum int
set @iTopNum = @PageSize * (@CurPage - 1)
set @SQL = @SQL + 'declare @sLastValue nvarchar(100) ' + char(13)
set @SQL = @SQL + 'SELECT Top ' + cast(@iTopNum as nvarchar(20)) + ' @sLastValue= ' + @KeyField + ' FROM ' + @TBName + @Condition + ' ' + @Order + char(13)