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

 更新时间:2020年6月25日 11:32  点击:2075

本文实例讲述了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#程序设计有所帮助。

[!--infotagslink--]

相关文章

  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • php KindEditor文章内分页的实例方法

    我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 自己动手写的jquery分页控件(非常简单实用)

    最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25