if(exists (select * from sys.objects where name='proc_Paging'))
drop proc proc_Paging
go
create proc proc_Paging
(
@CountryCode nvarchar(20),--国家编号
@PageIndex int =1,--当前要显示的页码
@PageSize int=3, --每页要显示的数据条数(页大小)
@TotalPages int out --总页数,一个输出参数
)
as
begin
--分页的SQL
select top (@PageSize) * from
(select row_number() over(order by ID) as rowIndex, * from city where CountryCode=@CountryCode) T
where rowIndex>((@PageIndex-1)*@PageSize)
drop proc proc_Paging
go
create proc proc_Paging
(
@CountryCode nvarchar(20),--国家编号
@PageIndex int =1,--当前要显示的页码
@PageSize int=3, --每页要显示的数据条数(页大小)
@TotalPages int out --总页数,一个输出参数
)
as
begin
--分页的SQL
select top (@PageSize) * from
(select row_number() over(order by ID) as rowIndex, * from city where CountryCode=@CountryCode) T
where rowIndex>((@PageIndex-1)*@PageSize)
--总数据量(既 一共有多少条数据)
declare @totalNum int --接收总数据量
select @totalNum =COUNT(1) from city where CountryCode=@CountryCode
declare @totalNum int --接收总数据量
select @totalNum =COUNT(1) from city where CountryCode=@CountryCode
--计算得出总页数
set @TotalPages =ceiling(@totalNum*1.0/@PageSize) --ceiling是向上取整
end
set @TotalPages =ceiling(@totalNum*1.0/@PageSize) --ceiling是向上取整
end
--=====================================================================================================================
--存储过程的调用
declare @num int
exec proc_Paging 'CHN',1,5,@num out
--存储过程的调用
declare @num int
exec proc_Paging 'CHN',1,5,@num out
print @num
/// <summary>
/// 分页的存储过程调用
/// </summary>
/// <returns></returns>
public List<AskBillModel> GetList(out int pageCount, string name = "", int pageIndex = 1, int pageSize = 0)
{
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter(){ ParameterName="@Name",SqlDbType=SqlDbType.NVarChar,SqlValue=name},
new SqlParameter(){ ParameterName="@PageIndex",SqlDbType=SqlDbType.Int,SqlValue=pageIndex},
new SqlParameter(){ ParameterName="@PageSize",SqlDbType=SqlDbType.Int,SqlValue=pageSize},
//Direction是指定参数的输入输出类型,Output代表输出参数,输出参数不需要给value值
new SqlParameter(){ ParameterName="@PageCount",SqlDbType=SqlDbType.Int,Direction=ParameterDirection.Output}
/// 分页的存储过程调用
/// </summary>
/// <returns></returns>
public List<AskBillModel> GetList(out int pageCount, string name = "", int pageIndex = 1, int pageSize = 0)
{
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter(){ ParameterName="@Name",SqlDbType=SqlDbType.NVarChar,SqlValue=name},
new SqlParameter(){ ParameterName="@PageIndex",SqlDbType=SqlDbType.Int,SqlValue=pageIndex},
new SqlParameter(){ ParameterName="@PageSize",SqlDbType=SqlDbType.Int,SqlValue=pageSize},
//Direction是指定参数的输入输出类型,Output代表输出参数,输出参数不需要给value值
new SqlParameter(){ ParameterName="@PageCount",SqlDbType=SqlDbType.Int,Direction=ParameterDirection.Output}
var list = DBHelper.GetListByProc<AskBillModel>("Proc_Paged", paras);
//一定要在执行了存储过程之后 再给输出的参数赋值
pageCount = Convert.ToInt32(paras[3].Value);
return list
};
//一定要在执行了存储过程之后 再给输出的参数赋值
pageCount = Convert.ToInt32(paras[3].Value);
return list
};
/// <summary>
/// 提供分页的相关数据
/// </summary>
/// <returns></returns>
[WebMethod] //这是WS调用
public PageModel GetPage(string name = "", int pageIndex = 1, int pageSize = 5)
{
PageModel model = new PageModel();
int count = 0;
model.BillList = dal.GetList(out count, name, pageIndex, pageSize);
model.PageCount = count;
return model;
}
/// 提供分页的相关数据
/// </summary>
/// <returns></returns>
[WebMethod] //这是WS调用
public PageModel GetPage(string name = "", int pageIndex = 1, int pageSize = 5)
{
PageModel model = new PageModel();
int count = 0;
model.BillList = dal.GetList(out count, name, pageIndex, pageSize);
model.PageCount = count;
return model;
}