[SQL]通用的分頁存儲過程

------------------------------------------------------------
--
--
通用的分頁存儲過程
--
--
-----------------------------------------------------------
CREATE PROCEDURE [dbo].[PageList]
(
@tblName     nvarchar(200),        ----要顯示的表或多個表的連接
@fldName     nvarchar(500= '*',    ----要顯示的字段列表
@pageSize    int = 1,        ----每頁顯示的記錄個數
@page        int = 10,        ----要顯示那一頁的記錄
@fldSort    nvarchar(200= null,    ----排序字段列表或條件
@Sort        bit = 0,        ----排序方法,0爲升序,1爲降序(如果是多字段排列Sort指代最後一個排序字段的排列順序(最後一個排序字段不加排序標記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')
@strCondition    nvarchar(1000= null,    ----查詢條件,不需where,以And開始
@ID        nvarchar(150),        ----主表的主鍵
@Dist                 bit = 0 ,          ----是否添加查詢字段的 DISTINCT 默認0不添加/1添加
@pageCount    int = 1 output,            ----查詢結果分頁後的總頁數
@Counts    int = 1 output,                ----查詢到的記錄數
@strSql          nvarchar(1000= '' output  -----最後返回的SQL語句
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000)        ----存放動態生成的SQL語句
Declare @strTmp nvarchar(1000)        ----存放取得查詢結果總數的查詢語句
Declare @strID     nvarchar(1000)        ----存放取得查詢開頭或結尾ID的查詢語句
Declare @strSortType nvarchar(10)    ----數據排序規則A
Declare @strFSortType nvarchar(10)    ----數據排序規則B
Declare @SqlSelect nvarchar(50)         ----對含有DISTINCT的查詢進行SQL構造
Declare @SqlCounts nvarchar(50)          ----對含有DISTINCT的總數查詢進行SQL構造
if @Dist  = 0
begin
    
set @SqlSelect = 'select '
    
set @SqlCounts = 'Count(*)'
end
else
begin
    
set @SqlSelect = 'select distinct '
    
set @SqlCounts = 'Count(DISTINCT '+@ID+')'
end
if @Sort=0
begin
    
set @strFSortType=' ASC '
    
set @strSortType=' DESC '
end
else
begin
    
set @strFSortType=' DESC '
    
set @strSortType=' ASC '
end
--------生成查詢語句--------
--
此處@strTmp爲取得查詢結果數量的語句
if @strCondition is null or @strCondition=''     --沒有設置顯示條件
begin
    
set @sqlTmp =  @fldName + ' From ' + @tblName
    
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
    
set @strID = ' From ' + @tblName
end
else
begin
    
set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
    
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
    
set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end
----取得查詢結果總數量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
declare @tmpCounts int
if @Counts = 0
    
set @tmpCounts = 1
else
    
set @tmpCounts = @Counts
    
--取得分頁總數
    set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
    
/**//**當前頁大於總頁數 取最後一頁**/
    
if @page>@pageCount
        
set @page=@pageCount
    
--/*-----數據分頁2分處理-------*/
    declare @pageIndex int --總數/頁大小
    declare @lastcount int --總數%頁大小 
    set @pageIndex = @tmpCounts/@pageSize
    
set @lastcount = @tmpCounts%@pageSize
    
if @lastcount > 0
        
set @pageIndex = @pageIndex + 1
    
else
        
set @lastcount = @pagesize
    
--//***顯示分頁
    if @strCondition is null or @strCondition=''     --沒有設置顯示條件
    begin
        
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分數據處理
            begin 
                
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
                        
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1as Varchar(20)) +' '+ @ID +' from '+@tblName
                        
+' order by '+ @fldSort +' '+ @strFSortType+')'
                        
+' order by '+ @fldSort +' '+ @strFSortType 
            
end
        
else
            
begin
            
set @page = @pageIndex-@page+1 --後半部分數據處理
                if @page <= 1 --最後一頁數據顯示
                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
                        
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
                
else                
                    
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
                        
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
                        
+' order by '+ @fldSort +' '+ @strSortType+')'
                        
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
            
end
    
end
    
else --有查詢條件
    begin
        
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分數據處理
        begin 
                
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from  '+@tblName
                    
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1as Varchar(20)) +' '+ @ID +' from '+@tblName
                    
+' Where (1>0) ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType+')'
                    
+' ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType                 
        
end
        
else
        
begin 
            
set @page = @pageIndex-@page+1 --後半部分數據處理
            if @page <= 1 --最後一頁數據顯示
                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
                        
+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
            
else
                    
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
                        
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
                        
+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+')'
                        
+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
        
end    
    
end
------返回查詢結果-----
set @strSql = @strTmp
exec sp_executesql @strTmp
--print @strTmp

 

 數據庫基類

 

//------------------------------------------------------------------------------
// 創建標識: Copyright (C) 2007 Socansoft.com 版權所有
// 創建描述: SocanCode代碼生成器自動創建
//
// 功能描述: 數據庫操作基類
//
// 修改標識:
// 修改描述:
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml;
using System.Reflection;


namespace DBUtility
{
    
/// <summary>
    
/// 數據訪問基礎類(基於SQLServer)
    
/// </summary>

    public abstract class SqlHelper
    
{
        
//數據庫連接字符串(web.config來配置)
        public static readonly string LocalSqlServer = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

        
/// <summary>
        
/// 通用分頁存儲過程
        
/// </summary>
        
/// <param name="connectionString">連接</param>
        
/// <param name="tblName">要顯示的表或多個表的連接</param>
        
/// <param name="fldName">要顯示的字段列表,可爲Null,表示*</param>
        
/// <param name="pageSize">每頁顯示的記錄個數</param>
        
/// <param name="pageIndex">要顯示那一頁的記錄</param>
        
/// <param name="fldSort">排序字段列表或條件</param>
        
/// <param name="Sort">排序方法,False爲升序,True爲降序(如果是多字段排列Sort指代最後一個排序字段的排列順序(最後一個排序字段不加排序標記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')</param>
        
/// <param name="strCondition">查詢條件,不需where,以And開始,可爲Null,表示""</param>
        
/// <param name="ID">主表的主鍵</param>
        
/// <param name="Disk">是否添加查詢字段的 DISTINCT 默認False不添加/True添加</param>
        
/// <param name="pageCount">查詢結果分頁後的總頁數</param>
        
/// <param name="Counts">查詢到的記錄數</param>
        
/// <param name="strSql">最後返回的SQL語句</param>
        
/// <returns>查詢當前頁的數據集</returns>

        public static DataSet PageList(string connectionString, string tblName, string fldName, int pageSize, int pageIndex,
            
string fldSort, bool Sort, string strCondition, string ID, bool Dist,
            
out int pageCount, out int Counts, out string strSql)
        
{
            SqlParameter[] parameters 
=new SqlParameter("@tblName",SqlDbType.NVarChar,200),
                
new SqlParameter("@fldName",SqlDbType.NVarChar,500),
                
new SqlParameter("@pageSize",SqlDbType.Int),
                
new SqlParameter("@page",SqlDbType.Int),
                
new SqlParameter("@fldSort",SqlDbType.NVarChar,200),
                
new SqlParameter("@Sort",SqlDbType.Bit),
                
new SqlParameter("@strCondition",SqlDbType.NVarChar,1000),
                
new SqlParameter("@ID",SqlDbType.NVarChar,150),
                
new SqlParameter("@Dist",SqlDbType.Bit),
                
new SqlParameter("@pageCount",SqlDbType.Int),
                
new SqlParameter("@Counts",SqlDbType.Int),
                
new SqlParameter("@strSql",SqlDbType.NVarChar,1000)}
;

            parameters[
0].Value = tblName;
            parameters[
1].Value = (fldName == null? "*" : fldName;
            parameters[
2].Value = (pageSize == 0? int.Parse(ConfigurationManager.AppSettings["PageSize"]) : pageSize;
            parameters[
3].Value = pageIndex;
            parameters[
4].Value = fldSort;
            parameters[
5].Value = Sort;
            parameters[
6].Value = strCondition == null ? "" : strCondition;
            parameters[
7].Value = ID;
            parameters[
8].Value = Dist;
            parameters[
9].Direction = ParameterDirection.Output;
            parameters[
10].Direction = ParameterDirection.Output;
            parameters[
11].Direction = ParameterDirection.Output;

            DataSet ds 
= RunProcedure(connectionString, "PageList", parameters, "ds");

            pageCount 
= (int)parameters[9].Value;
            Counts 
= (int)parameters[10].Value;
            strSql 
= parameters[11].Value.ToString();
            
return ds;
        }


        
執行簡單SQL語句 執行簡單SQL語句

        
執行帶參數的SQL語句 執行帶參數的SQL語句

        
存儲過程操作 存儲過程操作

        
構造語句常用類 構造語句常用類

        
由Object取值

        
序列化與反序列化

        
Model與XML互相轉換
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章