C#基于數(shù)據(jù)庫存儲(chǔ)過程的AJAX分頁實(shí)例
本文實(shí)例講述了C#基于數(shù)據(jù)庫存儲(chǔ)過程的AJAX分頁實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
首先我們在數(shù)據(jù)庫(SQL Server)中聲明定義存儲(chǔ)過程
if(exists(select * from sys.objects where name='proc_location_Paging')) --如果這個(gè)proc_location_paging存儲(chǔ)過程存在則刪除
drop proc proc_location_Paging
go
create proc proc_location_Paging --創(chuàng)建存儲(chǔ)過程
(
@pageSize int, --頁大小
@currentpage int, --當(dāng)前頁
@rowCount int output, --總行數(shù)(傳出參數(shù))
@pageCount int output --總頁數(shù)(傳出參數(shù))
)
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
---------------------------------以上就表示這個(gè)存儲(chǔ)過程已經(jīng)定義完了。
---------------------------------以下是執(zhí)行這個(gè)存儲(chǔ)過程。我們可以看結(jié)果
declare @rowCount int,@pageCount int --先聲明兩個(gè)參數(shù)
--執(zhí)行proc_location_Paging這個(gè)存儲(chǔ)過程。@rowCount,@pageCount后面都有output 表示它們兩是輸出參數(shù)
exec proc_location_Paging 10,1,@rowCount output,@pageCount output
select @rowCount,@pageCount --查詢這兩個(gè)參數(shù)的值
因?yàn)槭侵苯釉L問數(shù)據(jù)庫的,所以我們將下面這條方法寫入到DAL層中,這里我將它寫入到SqlHelper中
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>
/// 獲取連接數(shù)據(jù)庫字符串
/// </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"; //存儲(chǔ)過程的名字
cmd.CommandType = CommandType.StoredProcedure; //設(shè)置命令為存儲(chǔ)過程類型(即:指明我們執(zhí)行的是一個(gè)存儲(chǔ)過程)
rowCount = 0;
pageCount = 0;//這里隨便給rowCount,pageCount賦個(gè)值,因?yàn)槭褂胦ut傳遞參數(shù)的時(shí)候,在方法內(nèi)部一定要給out參數(shù)賦值才能用它,但是雖然這里給它賦初值了,但是在執(zhí)行存儲(chǔ)過程中,存儲(chǔ)過程又會(huì)給這兩個(gè)參數(shù)賦值,并返還回來給我們,那個(gè)才是我們要值
SqlParameter[] parameters ={
new SqlParameter("@pageSize",pageSize),
new SqlParameter("@currentpage",currentPage),
new SqlParameter("@rowCount",rowCount),
new SqlParameter("@pageCount",pageCount)
};
//因?yàn)樵诖鎯?chǔ)過程中@rowCount 與@pageCount 是一個(gè)輸出參數(shù)(output), 而parameters這個(gè)數(shù)組里,第三,和第四個(gè)參數(shù)就是要用來替換掉這兩個(gè)輸出參數(shù)的,所以這里要將parameters這個(gè)數(shù)組里的這兩個(gè)參數(shù)設(shè)為輸出參數(shù)。
parameters[2].Direction = ParameterDirection.Output;
parameters[3].Direction = ParameterDirection.Output;
cmd.Parameters.AddRange(parameters); //將參數(shù)傳遞給我們的cmd命令對(duì)象
DataTable dt = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(dt);//到數(shù)據(jù)庫去執(zhí)行存儲(chǔ)過程,并將結(jié)果填充到dt表中
}
//等存儲(chǔ)過程執(zhí)行完畢后,存儲(chǔ)過程會(huì)把這兩個(gè)輸出參數(shù)傳遞出來。那么我們在這里來取得這兩個(gè)返回參數(shù)。
rowCount = Convert.ToInt32(parameters[2].Value);
pageCount = Convert.ToInt32(parameters[3].Value);
return dt;
}
}
}
}
}
在DAL文件夾中( 層中) 創(chuàng)建一個(gè)Aticel.cs類 產(chǎn)生一個(gè)list
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>();// 聲明一個(gè)泛型對(duì)象list
if (dt != null && dt.Rows.Count > 0)
{
//將DataTable轉(zhuǎn)換成一個(gè)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; //將這個(gè)list返回回去
}
}
}
在API這個(gè)文件夾中創(chuàng)建一個(gè)GetPageData.ashx 頁 (BLL層) 在這里調(diào)用ADL層里的 Aticel.cs類中的GetPageListByPageIndex()方法,獲取一個(gè)list 并將這個(gè)list轉(zhuǎn)換成一個(gè)Json格式字符串, 共AJAX 異步請求得到。
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>
/// 根據(jù)用戶傳遞的當(dāng)前頁的頁碼來獲取數(shù)據(jù)
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int pageSize = 10; //設(shè)定頁大小,每頁顯示10條數(shù)據(jù)
int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"]); //設(shè)定當(dāng)前頁
int rowCount = 0; //作為out參數(shù)傳遞給方法,在方法里給rowCount賦值
int pageCount = 0; //作為out參數(shù)傳遞給方法,在方法里給rowCount賦值
string jsonData = null;
List<Model.Location> list= DAL.Aticel.GetPageListByPageIndex(pageSize, currentPage, out rowCount, out pageCount);
if (list != null && list.Count > 0)
{
//創(chuàng)建Json序列化器,將對(duì)象轉(zhuǎn)換成一個(gè)Json格式的字符串
JavaScriptSerializer jsz = new JavaScriptSerializer();
jsonData = jsz.Serialize(list); //將一個(gè)list對(duì)象轉(zhuǎn)換成json格式的字符串
context.Response.Write(jsonData);
}
else
{
context.Response.Write("no");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
前端頁面 (將AJAX請求得到的數(shù)據(jù)展示也頁面)
<!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) { //假設(shè)當(dāng)前頁是第二頁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>編號(hào)</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>
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#中List轉(zhuǎn)IList的實(shí)現(xiàn)
本文主要介紹了C#中List轉(zhuǎn)IList的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07c# winform異步不卡界面的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于c# winform異步不卡界面的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色
這篇文章介紹了C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹
這篇文章介紹了C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件
這篇文章主要介紹了WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件,需要的朋友可以參考下2014-08-08