AJAX分页 (内容涉及到 存储过程)
发布时间:2020-12-13 20:12:19 所属栏目:PHP教程 来源:网络整理
导读:1 首先我们在数据库(SQL Server)中声明定义存储进程 use sales --指定数据库if(exists(select * from sys.objects where name=proc_location_Paging)) --如果这个proc_location_paging存储进程存在则删除drop proc proc_location_Paginggocreate proc proc
<1> 首先我们在数据库(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⑴))
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;
using System.Data;
using System.Reflection;
namespace LLSql.DAL
{
public class SqlHelper
{
/// <summary>
/// 获得连接数据库字符串
/// </summary>
private static string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
public static DataTable ExecuteProcPageList(int pageSize,int currentPage,out int rowCount,out int pageCount)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "proc_location_paging"; //存储进程的名字
cmd.CommandType = CommandType.StoredProcedure; //设置命令为存储进程类型(即:指明我们履行的是1个存储进程)
rowCount = 0;
pageCount = 0;//这里随意给rowCount,pageCount赋个值,由于使用out传递参数的时候,在方法内部1定要给out参数赋值才能用它,但是虽然这里给它赋初值了,但是在履行存储进程中,存储进程又会给这两个参数赋值,并返还回来给我们,那个才是我们要值
SqlParameter[] parameters ={
new SqlParameter("@pageSize",pageSize),new SqlParameter("@currentpage",currentPage),new SqlParameter("@rowCount",rowCount),new SqlParameter("@pageCount",pageCount)
};
//由于在存储进程中@rowCount 与@pageCount 是1个输出参数(output),而parameters这个数组里,第3,和第4个参数就是要用来替换掉这两个输出参数的,所以这里要将parameters这个数组里的这两个参数设为输出参数。
parameters[2].Direction = ParameterDirection.Output;
parameters[3].Direction = ParameterDirection.Output;
cmd.Parameters.AddRange(parameters); //将参数传递给我们的cmd命令对象
DataTable dt = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(dt);//到数据库去履行存储进程,并将结果填充到dt表中
}
//等存储进程履行终了后,存储进程会把这两个输出参数传递出来。那末我们在这里来获得这两个返回参数。
rowCount = Convert.ToInt32(parameters[2].Value);
pageCount = Convert.ToInt32(parameters[3].Value);
return dt;
}
}
}
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |