[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 - 1 as   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 - 1 as   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互相转换
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值