最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

復(fù)制代碼 代碼如下:
use sales    --指定數(shù)據(jù)庫 
 
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   --創(chuàng)建存儲過程 

@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 
---------------------------------以上就表示這個存儲過程已經(jīng)定義完了。 
 
---------------------------------以下是執(zhí)行這個存儲過程。我們可以看結(jié)果 
 
declare @rowCount int,@pageCount int  --先聲明兩個參數(shù) 
 
--執(zhí)行proc_location_Paging這個存儲過程。@rowCount,@pageCount后面都有output 表示它們兩是輸出參數(shù) 
exec proc_location_Paging 10,1,@rowCount output,@pageCount output   
 
select @rowCount,@pageCount  --查詢這兩個參數(shù)的值

因為是直接訪問數(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"; //存儲過程的名字 
                    cmd.CommandType = CommandType.StoredProcedure; //設(shè)置命令為存儲過程類型(即:指明我們執(zhí)行的是一個存儲過程)
                    rowCount = 0; 
                    pageCount = 0;//這里隨便給rowCount,pageCount賦個值,因為使用out傳遞參數(shù)的時候,在方法內(nèi)部一定要給out參數(shù)賦值才能用它,但是雖然這里給它賦初值了,但是在執(zhí)行存儲過程中,存儲過程又會給這兩個參數(shù)賦值,并返還回來給我們,那個才是我們要值 
                    SqlParameter[] parameters ={ 
                             new SqlParameter("@pageSize",pageSize), 
                             new SqlParameter("@currentpage",currentPage), 
                             new SqlParameter("@rowCount",rowCount), 
                             new SqlParameter("@pageCount",pageCount) 
                    }; 
                    //因為在存儲過程中@rowCount 與@pageCount 是一個輸出參數(shù)(output), 而parameters這個數(shù)組里,第三,和第四個參數(shù)就是要用來替換掉這兩個輸出參數(shù)的,所以這里要將parameters這個數(shù)組里的這兩個參數(shù)設(shè)為輸出參數(shù)。 
                    parameters[2].Direction = ParameterDirection.Output;
                    parameters[3].Direction = ParameterDirection.Output;
                    cmd.Parameters.AddRange(parameters); //將參數(shù)傳遞給我們的cmd命令對象 

                    DataTable dt = new DataTable(); 
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) 
                    { 
                        adapter.Fill(dt);//到數(shù)據(jù)庫去執(zhí)行存儲過程,并將結(jié)果填充到dt表中 
                    } 
                    //等存儲過程執(zhí)行完畢后,存儲過程會把這兩個輸出參數(shù)傳遞出來。那么我們在這里來取得這兩個返回參數(shù)。 
                    rowCount = Convert.ToInt32(parameters[2].Value); 
                    pageCount = Convert.ToInt32(parameters[3].Value); 
                    return dt; 
                } 
            } 
        } 
    } 
}

在DAL文件夾中( 層中) 創(chuàng)建一個Aticel.cs類  產(chǎn)生一個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>();// 聲明一個泛型對象list 
            if (dt != null && dt.Rows.Count > 0) 
            { 
                //將DataTable轉(zhuǎn)換成一個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這個文件夾中創(chuàng)建一個GetPageData.ashx 頁 (BLL層) 在這里調(diào)用ADL層里的 Aticel.cs類中的GetPageListByPageIndex()方法,獲取一個list  并將這個list轉(zhuǎn)換成一個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序列化器,將對象轉(zhuǎn)換成一個Json格式的字符串 
                JavaScriptSerializer jsz = new JavaScriptSerializer(); 
                jsonData = jsz.Serialize(list); //將一個list對象轉(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>編號</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#程序設(shè)計有所幫助。

相關(guān)文章

  • C#多線程死鎖介紹與案例代碼

    C#多線程死鎖介紹與案例代碼

    這篇文章介紹了C#多線程的死鎖,并使用案例代碼實現(xiàn)解決方案,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#獲取攝像頭拍照顯示圖像的方法

    C#獲取攝像頭拍照顯示圖像的方法

    這篇文章主要為大家詳細(xì)介紹了C#獲取攝像頭拍照顯示圖像的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C#中整理了幾種字符串截取方法小結(jié)

    C#中整理了幾種字符串截取方法小結(jié)

    本文給大家整理了幾種字符串截取方法,?(Substring);(Remove);(Replace)方法和split方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • c#操作iis根目錄的方法

    c#操作iis根目錄的方法

    這篇文章主要介紹了c#操作iis根目錄的方法,涉及C#針對IIS下目錄的相關(guān)操作技巧,需要的朋友可以參考下
    2015-06-06
  • C#使用RegNotifyChangeKeyValue監(jiān)聽注冊表更改的方法小結(jié)

    C#使用RegNotifyChangeKeyValue監(jiān)聽注冊表更改的方法小結(jié)

    RegNotifyChangeKeyValue的最后一個參數(shù)傳遞false,表示以同步的方式監(jiān)聽,這篇文章主要介紹了C#使用RegNotifyChangeKeyValue監(jiān)聽注冊表更改的方法小結(jié),需要的朋友可以參考下
    2024-06-06
  • 用C#的params關(guān)鍵字實現(xiàn)方法形參個數(shù)可變示例

    用C#的params關(guān)鍵字實現(xiàn)方法形參個數(shù)可變示例

    params關(guān)鍵字以實現(xiàn)方法形參個數(shù)可變是C#語法的一大優(yōu)點,下面是用C#中的params關(guān)鍵字實現(xiàn)方法形參個數(shù)可變
    2014-09-09
  • C#實現(xiàn)基于XML配置MenuStrip菜單的方法

    C#實現(xiàn)基于XML配置MenuStrip菜單的方法

    這篇文章主要介紹了C#實現(xiàn)基于XML配置MenuStrip菜單的方法,涉及C#使用XML配置MenuStrip菜單的原理與實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • 基于WPF實現(xiàn)控件輪廓跑馬燈動畫效果

    基于WPF實現(xiàn)控件輪廓跑馬燈動畫效果

    這篇文章主要介紹了如何利用WPF實現(xiàn)控件輪廓跑馬燈動畫效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-08-08
  • C#多線程之線程池(ThreadPool)

    C#多線程之線程池(ThreadPool)

    這篇文章介紹了C#多線程之線程池(ThreadPool)的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#中委托用法實例分析

    C#中委托用法實例分析

    這篇文章主要介紹了C#中委托用法,較為詳細(xì)的分析了C#中委托的概念與相關(guān)的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-05-05

最新評論

彰化市| 南靖县| 宁河县| 元朗区| 陵水| 康定县| 沈丘县| 天镇县| 晋州市| 高青县| 通州区| 班玛县| 嘉禾县| 张家川| 贵港市| 巴马| 沙洋县| 蓝田县| 丰台区| 广元市| 正定县| 万全县| 莱芜市| 洛隆县| 洛川县| 佳木斯市| 剑河县| 三都| 崇礼县| 海伦市| 阿城市| 洞口县| 新乡县| 厦门市| 甘南县| 依安县| 乌拉特后旗| 读书| 玛纳斯县| 根河市| 房山区|