C#基于数据库存储过程的AJAX分页实例

2019-12-26 12:06:16王旭
易采站长站为您分析C#基于数据库存储过程的AJAX分页实现方法,以实例形式详细讲述了数据库存储过程的定义、数据库的访问及Ajax的实现技巧,需要的朋友可以参考下    

本文实例讲述了C#基于数据库存储过程的AJAX分页实现方法。。具体如下:

首先我们在数据库(SQL Server)中声明定义存储过程

复制代码 use sales    --指定数据库  
  
if(exists(select * from sys.objects where name='proc_location_Paging')) --如果这个proc_location_paging存储过程存在则删除  
drop proc proc_location_Paging  
go  
  
create proc proc_location_Paging   --创建存储过程  
(  
@pageSize int,  --页大小  
@currentpage int,  --当前页  
@rowCount int output,  --总行数(传出参数)  
@pageCount int output  --总页数(传出参数)  
)  
as  
begin  
  
select @rowCount= COUNT(locid) from location  --给@rowCount赋值  
  
select @pageCount= CEILING((count(locid)+0.0)/@pageSize) from location  --给@pageCount赋值  
  
select top (@pagesize)* from (select ROW_NUMBER() over(order by locid) as rowID,* from location) as t1  
where rowID >(@pageSize*(@currentpage-1))  
  
end  
go  
---------------------------------以上就表示这个存储过程已经定义完了。  
  
---------------------------------以下是执行这个存储过程。我们可以看结果  
  
declare @rowCount int,@pageCount int  --先声明两个参数  
  
--执行proc_location_Paging这个存储过程。@rowCount,@pageCount后面都有output 表示它们两是输出参数  
exec proc_location_Paging 10,1,@rowCount output,@pageCount output    
  
select @rowCount,@pageCount  --查询这两个参数的值

 

因为是直接访问数据库的,所以我们将下面这条方法写入到DAL层中,这里我将它写入到SqlHelper中

复制代码 using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Configuration;  
using System.Data.SqlClient;