日期:2014-05-17 浏览次数:20533 次
ALTER proc [dbo].[pager]
@pageIndex int, --第几页
@pageSize int, --每页显示几行
@primaryKey varchar(1000), --主键
@tableName varchar(2000), --表名
@strWhere varchar(3000), --查询条件
@returnColumn varchar(1000),--返回列
@orderField varchar(1000), --排序字段
@orderType varchar(1000), --排序类别 desc asc
@tolPage int output, --总页数
@tolRecord int output --总条数
as
declare @sql nvarchar(4000)
if(@strwhere='')
set @sql='select @total=count(*) from '+@tableName
else
set @sql='select @total=count(*) from '+@tableName+' where '+@strWhere
exec sp_executesql @sql,N'@total int output',@tolRecord output
set @tolPage=ceiling(@tolRecord*1.0/@pageSize)
if(@strWhere='')
set @sql='select top '+convert(varchar(50),@pageSize)
+' '+@returnColumn+' from '+@tableName+' where '+@primaryKey
+' not in(select top '+convert(varchar(50),(@pageIndex-1)*@pageSize)
+' '+@primaryKey+' from '+@tableName+' order by '+@orderField+' '
+@orderType+') order by '+@orderField+' '+@orderType
else
set @sql='select top '+convert(varchar(50),@pageSize)
+' '+@returnColumn+' from '+@tableName+' where('+@strWhere+') and ('
+@primaryKey+' not in(select top '+convert(varchar(50),(@pageIndex-1)*@pageSize)
+' '+@primaryKey+' from '+@tableName+' where '+@strWhere+' order by '+@orderField+' '
+' '+@orderType+')) order by '+@orderField+' '+@orderType
print @sql
exec (@sql)
public static DataTable SelectPage(int pageIndex, int pageSize, string primaryKey, string tableName,
string strWhere, string returnColumn, string orderField, string orderType, out int tolPage,
out int tolRecord)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandText = "pager";
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Connection = connection;
sqlcmd.Parameters.Add(new SqlParameter("@pageIndex", pageIndex));
sqlcmd.Parameters.Add(new SqlParameter("@pageSize", pageSize));
sqlcmd.Parameter