C#基于数据库存储过程的AJAX分页实例
作者:shichen2014 发布时间:2024-01-26 20:43:23
本文实例讲述了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;
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; //设置命令为存储过程类型(即:指明我们执行的是一个存储过程)
rowCount = 0;
pageCount = 0;//这里随便给rowCount,pageCount赋个值,因为使用out传递参数的时候,在方法内部一定要给out参数赋值才能用它,但是虽然这里给它赋初值了,但是在执行存储过程中,存储过程又会给这两个参数赋值,并返还回来给我们,那个才是我们要值
SqlParameter[] parameters ={
new SqlParameter("@pageSize",pageSize),
new SqlParameter("@currentpage",currentPage),
new SqlParameter("@rowCount",rowCount),
new SqlParameter("@pageCount",pageCount)
};
//因为在存储过程中@rowCount 与@pageCount 是一个输出参数(output), 而parameters这个数组里,第三,和第四个参数就是要用来替换掉这两个输出参数的,所以这里要将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;
}
}
}
}
}
在DAL文件夹中( 层中) 创建一个Aticel.cs类 产生一个list
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using LLSql.DAL;
using WebApplication1.Model;
namespace WebApplication1.DAL
{
public class Aticel
{
public static List<Location> GetPageListByPageIndex(int pageSize,int currentpage,out int rowCount,out int pageCount)
{
DataTable dt= SqlHelper.ExecuteProcPageList(pageSize, currentpage,out rowCount,out pageCount);
var list = new List<Location>();// 声明一个泛型对象list
if (dt != null && dt.Rows.Count > 0)
{
//将DataTable转换成一个list
list = (from p in dt.AsEnumerable() //(遍历DataTable)
select new Model.Location
{
Locid = p.Field<int>("locid"), //将DateTable里的字段赋值给Location类中的属性
LocName = p.Field<string>("locName"),
ParentId = p.Field<int>("parentId"),
LocType = p.Field<short>("locType"),
ElongCode = p.Field<string>("elongCode"),
CityCode = p.Field<string>("CityCode"),
BaiduPos = p.Field<string>("BaiduPos"),
Versions = p.Field<short>("Version")
}).ToList();
}
return list; //将这个list返回回去
}
}
}
在API这个文件夹中创建一个GetPageData.ashx 页 (BLL层) 在这里调用ADL层里的 Aticel.cs类中的GetPageListByPageIndex()方法,获取一个list 并将这个list转换成一个Json格式字符串, 共AJAX 异步请求得到。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
namespace WebApplication1.API
{
/// <summary>
/// GetPageData 的摘要说明
/// </summary>
public class GetPageData : IHttpHandler
{
/// <summary>
/// 根据用户传递的当前页的页码来获取数据
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int pageSize = 10; //设定页大小,每页显示10条数据
int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"]); //设定当前页
int rowCount = 0; //作为out参数传递给方法,在方法里给rowCount赋值
int pageCount = 0; //作为out参数传递给方法,在方法里给rowCount赋值
string jsonData = null;
List<Model.Location> list= DAL.Aticel.GetPageListByPageIndex(pageSize, currentPage, out rowCount, out pageCount);
if (list != null && list.Count > 0)
{
//创建Json序列化器,将对象转换成一个Json格式的字符串
JavaScriptSerializer jsz = new JavaScriptSerializer();
jsonData = jsz.Serialize(list); //将一个list对象转换成json格式的字符串
context.Response.Write(jsonData);
}
else
{
context.Response.Write("no");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
前端页面 (将AJAX请求得到的数据展示也页面)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>使用AJAX分页</title>
<script src="jquery-1.11.2.js" type="text/javascript"></script>
<style type="text/css">
table{ margin:80px 500px; }
td{ width:50px; height:auto}
</style>
<script type="text/javascript">
$(function () {
$.get("API/GetPageData.ashx?currentPage=2", function (obj) { //假设当前页是第二页currentPage=2
//debugger;
var JsonData = $.parseJSON(obj);
//alert(JsonData[0].Locid);
//debugger;
for (var i = 0; i < JsonData.length; i++) {
var data = "<tr><td >" + JsonData[i].Locid + "</td><td >" + JsonData[i].LocName + "</td><td >" + JsonData[i].ParentId + "</td><td >" + JsonData[i].LocType + "</td><td >" + JsonData[i].ElongCode + "</td><td >" + JsonData[i].CityCode + "</td><td >" + JsonData[i].BaiduPos + "</td><td >" + JsonData[i].Versions + "</td></tr>";
$("#t1").append(data);
}
})
})
</script>
</head>
<body>
<table border="1" cellpadding="5" cellspacing="0" style="margin-top:100px;width:600px;" id="t1">
<tr><td>编号</td><td >城市名</td><td >父ID</td><td >locType</td><td >elongCode</td><td >CityCode</td><td >BaiduPos</td><td >Version</td></tr>
</table>
</body>
</html>
希望本文所述对大家的C#程序设计有所帮助。


猜你喜欢
- 1,System.ComponentModelSystem.ComponentModel 命名空间提供用于实现组件和控件的运行时和设计时行为
- 引用PyMongo>>> import pymongo创建连接Connection>>> import
- 项目背景最近在做的项目,涉及到数据库的操作了,之前做的是直接调用接口,不用做存库操作。因此要增加大量特殊格式的实体类。比如我们用的是 JPA
- 生成静态页的页面非常的简单就是定义好模板与模板标题,之后利用str_replace进行替换了,是最常用的方法,另一种是利用ob_get_co
- 希腊Web 设计师Christos Chiotis 发表在 CssGlobe 的一篇文章,讲述了黄金分割率在 CSS 中的应用。黄金分割率是
- 前言: 最近正在上一门Python数据处理的课程,要用到Jupyter,于是就先安装了
- JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它采用完全独立于语言的文本格式,可替换XML成
- 在观看唐宇迪老师图像处理的课程中,其中有一个答题卡识别的小项目,在此结合自己理解做一个简单的总结。1. 项目分析首先在拿到项目时候,分析项目
- 如果用户输入的是直接插入到一个SQL语句中的查询,应用程序会很容易受到SQL注入,例如下面的例子:$unsafe_variable = $_
- 1. 安装yaml库想要使用python实现yaml与json格式互相转换,需要先下载pip,再通过pip安装yaml库。如何下载以及使用p
- 本文为大家分享了mysql 8.0.15 winx64压缩包安装配置方法,供大家参考,具体内容如下1、在官网下载压缩包2、解压缩包3、增加配
- 前言我们在日常文本处理中,经常会将数据结构保存在列表中,如果将列表中的项进行关联,创建我们想要的字典结构,存取就会十分方便!示例详解比如说将
- 解决办法1. 卸载重装,不设root密码,因为MySql默认密码为空。 解决办法2. 保持现在的密码,打开“MySQL Command Li
- 本文实例讲述了Python使用cx_Oracle调用Oracle存储过程的方法。分享给大家供大家参考,具体如下:这里主要测试在Python中
- 在数据库使用中经常使用到时间字段。常用的有创建时间和更新时间。然而在使用中想要创建时间在创建的时候自动设置为当前时间,更新时间在更新时自动更
- 题目:用 JavaScript 代码实现空位补零,比如 pad(12, 3) => 012实现一:/* 平淡无奇法 */functio
- 本文主要给大家介绍了关于CentOS 6.5 安装Python 3.5.2并与Python2并存的相关内容,分享出来供大家参考学习,下面来看
- 1.apache配置文件中打开vhost的配置LoadModule vhost_alias_module modules/mod_vhost
- 生产图片区域,上传按钮#btn可替换自己想要的图片<ul id="ul_pics" class="ul_
- 在整个安装的过程中也遇到了很多的坑,故此做个记录,争取下次不再犯!我的整个基本配置如下:电脑环境如下:win10(64位)+CPU:E5-2