标签:
数据库记录分页列表是大量必须使用的基本技术,因此本文建议你在每个数据库中建立下面的存储过程:
CREATE PROCEDURE xsp_ination
(
@tblName varchar(64),
@strGetFields varchar(256) = ‘*‘,
@fldName varchar(64)=‘‘,
@PageSize int = 20,
@PageIndex int = 1,
@OrderType bit = 1,
@strWhere varchar(256) = ‘‘
)
AS
BEGIN
declare @strSQL varchar(1000)
declare @strTmp varchar(110)
declare @strOrder varchar(400)
SET NOCOUNT ON
if @OrderType != 0
begin
set @strTmp = ‘<(select min‘
set @strOrder = ‘ order by [‘ + @fldName +‘] desc‘
end
else
begin
set @strTmp = ‘>(select max‘
set @strOrder = ‘ order by [‘ + @fldName +‘] asc‘
end
if @PageIndex = 1
begin
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
end
else
begin
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 + ‘ ‘ + @strOrder
end
EXEC (@strSQL)
if @@error=0 return 1
SET NOCOUNT OFF
END
GO
使用方法是(C#):
sql = "EXEC [dbo].[xsp_ination] \"tblNEWS\",\"*\",\"id\",40," + pindex.ToString() + ",1,\"iType=" + type.ToString();
SqlDataReader sr = ExecuteReader(sql);
while (sr.Read())
{
...
}
sr.Close();
标签:
原文地址:http://www.cnblogs.com/luyiwei/p/4277393.html