欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#基于數(shù)據(jù)庫存儲(chǔ)過程的AJAX分頁實(shí)例

 更新時(shí)間:2015年01月21日 11:25:28   投稿:shichen2014  
這篇文章主要介紹了C#基于數(shù)據(jù)庫存儲(chǔ)過程的AJAX分頁實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)講述了數(shù)據(jù)庫存儲(chǔ)過程的定義、數(shù)據(jù)庫的訪問及Ajax的實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了C#基于數(shù)據(jù)庫存儲(chǔ)過程的AJAX分頁實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:

首先我們在數(shù)據(jù)庫(SQL Server)中聲明定義存儲(chǔ)過程

復(fù)制代碼 代碼如下:
use sales    --指定數(shù)據(jù)庫 
 
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中

復(fù)制代碼 代碼如下:
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> 
        /// 獲取連接數(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

復(fù)制代碼 代碼如下:
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>();// 聲明一個(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 異步請求得到。

復(fù)制代碼 代碼如下:
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> 
        /// 根據(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ù)展示也頁面)

復(fù)制代碼 代碼如下:
<%@ 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) { //假設(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# XML操作類分享

    C# XML操作類分享

    這篇文章主要分享了C# XML操作類的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#中List轉(zhuǎn)IList的實(shí)現(xià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-07
  • C# 操作符之二 算數(shù)操作符

    C# 操作符之二 算數(shù)操作符

    算數(shù)操作符+,-,*,/,%,的用法和大多程序設(shè)計(jì)語言是相同的,%號(hào)的作用是整數(shù)除法中產(chǎn)生余數(shù),而整數(shù)法會(huì)直接去掉小數(shù)部分,而不是四舍五入。
    2011-02-02
  • c# winform異步不卡界面的實(shí)現(xiàn)方法

    c# 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-07
  • C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色

    C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色

    這篇文章介紹了C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹

    C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹

    這篇文章介紹了C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件

    WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件

    這篇文章主要介紹了WinForm自定義函數(shù)FindControl實(shí)現(xiàn)按名稱查找控件,需要的朋友可以參考下
    2014-08-08
  • c#序列化詳解示例

    c#序列化詳解示例

    序列化是將對(duì)象狀態(tài)轉(zhuǎn)換為可保持或傳輸?shù)母袷降倪^程。與序列化相對(duì)的是反序列化,它將流轉(zhuǎn)換為對(duì)象。這兩個(gè)過程結(jié)合起來,可以輕松地存儲(chǔ)和傳輸數(shù)據(jù)
    2014-02-02
  • c# 繪制中國象棋棋盤與棋子

    c# 繪制中國象棋棋盤與棋子

    這篇文章主要介紹了c# 繪制中國象棋棋盤與棋子,文中實(shí)例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C# AttributeUsage使用案例詳解

    C# AttributeUsage使用案例詳解

    這篇文章主要介紹了C# AttributeUsage使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評(píng)論