先发自已写的
sql2005存储过程(可对视图分页,任何排序,效率高):
ALTER procedure [dbo].[QueueDrawMoney_Page] (@startIndex int,@endIndex int,@orderBy varchar(255),@strWhere varchar(1000),@tableName varchar(255),@IsReCount bit)ASdeclare @strSQL varchar(6000)
set @strSQL = 'with temptbl as ( SELECT ROW_NUMBER() OVER (ORDER BY '+@orderBy+')AS Row, * from '+@tableName+') SELECT * FROM temptbl where Row between '+str(@startIndex)+' and '+str(@endIndex)
if @strWhere!=''begin set @strSQL = 'with temptbl as ( SELECT ROW_NUMBER() OVER (ORDER BY '+@orderBy+')AS Row, * from '+@tableName+' WHERE '+@strWhere +')SELECT * FROM temptbl where Row between '+str(@startIndex)+' and '+str(@endIndex)
end
if @IsReCount != 0 set @strSQL = 'select count(*) as Total from [' + @tableName + ']'+' where ' + @strWhere
exec (@strSQL)
C#代码:
public DataSet GetList(int startIndex, int endIndex, string orderBy, string strWhere, string tableName) { SqlParameter[] parameters = { new SqlParameter("@startIndex", SqlDbType.Int), new SqlParameter("@endIndex", SqlDbType.Int), new SqlParameter("@orderBy", SqlDbType.VarChar,255), new SqlParameter("@strWhere", SqlDbType.VarChar,1000), new SqlParameter("@IsReCount", SqlDbType.Bit), new SqlParameter("@tableName", SqlDbType.VarChar,255), }; parameters[0].Value = startIndex; parameters[1].Value = endIndex; parameters[2].Value = orderBy; parameters[3].Value = strWhere; parameters[4].Value = false; parameters[5].Value = tableName; return DbHelperSQL.RunProcedure("QueueDrawMoney_Page", parameters, "ds"); }
sql 2000存储过程(到处都是效率高):
USE [Coopur]GO/****** 对象: StoredProcedure [dbo].[UP_GetRecordByPage] 脚本日期: 01/09/2011 22:35:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO
ALTER PROCEDURE [dbo].[UP_GetRecordByPage] @tblName varchar(255), -- 表名 @fldName varchar(255), -- 主键字段名 @PageSize int = 10, -- 页尺寸 @PageIndex int = 1, -- 页码 @IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回 @OrderType bit = 1, -- 设置排序类型, 非 0 值则降序 @strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)AS
declare @strSQL varchar(6000) -- 主语句declare @strTmp varchar(1000) -- 临时变量(查询条件过长时可能会出错,可修改100为1000)declare @strOrder varchar(400) -- 排序类型
if @OrderType = 0begin set @strTmp = '<(select min' set @strOrder = ' order by [' + @fldName +'] desc'endelsebegin set @strTmp = '>(select max' set @strOrder = ' order by [' + @fldName +'] asc'end
set @strSQL = 'select top ' + str(@PageSize) + ' * 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) + ' * from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1begin set @strTmp ='' if @strWhere != '' set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + ']' + @strTmp + ' ' + @strOrderend
if @IsReCount != 0 set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
exec (@strSQL)