標籤:滑鼠移動 學習 space namespace bsp order by 引用 ram row
本文執行個體講述了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;
}
}
}
}
}
除聲明外,
跑步客文章均為原創,轉載請以連結形式標明本文地址
C#基於資料庫預存程序的AJAX分頁執行個體
本文地址: http://www.paobuke.com/develop/c-develop/pbk23173.html
相關內容C#匯出Excel的樣本詳解C#控制台基礎 list初始化的兩種方法Winform實現滑鼠可穿透的表單鏤空效果關於finalize機制和引用、引用隊列的用法詳解
輕鬆學習C#的foreach迭代語句C#實現計算一個點圍繞另一個點旋轉指定弧度後座標值的方法C#編寫DES加密、解密類C#實現滑鼠移動到曲線圖上顯示值的方法
C#基於資料庫預存程序的AJAX分頁執行個體