惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
博客园 - 叶小钗
S
SegmentFault 最新的问题
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
L
Lohrmann on Cybersecurity
量子位
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
J
Java Code Geeks
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 三生石上(FineUI控件)
Attack and Defense Labs
Attack and Defense Labs
AI
AI
The Cloudflare Blog
T
Tailwind CSS Blog
S
Schneier on Security
爱范儿
爱范儿
PCI Perspectives
PCI Perspectives
Stack Overflow Blog
Stack Overflow Blog
S
Secure Thoughts
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
V2EX - 技术
V2EX - 技术
S
Securelist
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Help Net Security
Help Net Security
C
Cisco Blogs
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
K
Kaspersky official blog
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - 天龙

ASP.NET 中关于 Async 和 Await 的概述 WIN2003的IIS6调试ASP出现HTTP 500 - 内部服务器错误 SEO 工具集合 去除网页FLASH"单击并激活此控件"解决办法 - 天龙 - 博客园 SQL远程备份 - 天龙 - 博客园 Web Service测试工具小汇 [转]用一个JS代码实现页面刷新后保持页面滚动条的位置 - 天龙 - 博客园 删除SQL 日志 纯HTML也能访问数据库 - 天龙 - 博客园 关于net2.0里面新出现的类backgroundworker的应用 使用CHARINDEX函数提高查询速度 如果为网站生成自签名SSL证书 利用html设计滚动的文字 - 天龙 - 博客园 使IIS支持HTTPS/SSL A scrollable panel retaining its scroll position across postbacks. - 天龙 google搜索参数 五种提高SQL Server性能的方法 解析Atlas—微软的Ajax工具包 在ASP.NET 2.0工程中加入Atlas
SQL2分查找法通用分页存储过程算法
天龙 · 2006-11-05 · via 博客园 - 天龙

using System;

namespace CountryPark.DAL
{
    
/**//// <summary>
    
/// PageList 的摘要说明。
    
/// </summary>

    public sealed class PageList
    
{
        
static PageList()
        
{
        }

        
        
/**//// <summary>
        
/// 分页查询数据记录总数获取
        
/// </summary>
        
/// <param name="_tbName">----要显示的表或多个表的连接</param>
        
/// <param name="_ID">----主表的主键</param>
        
/// <param name="_strCondition">----查询条件,不需where</param>        
        
/// <param name="_Dist">----是否添加查询字段的 DISTINCT 默认0不添加/1添加</param>
        
/// <returns></returns>

        public static string getPageListCounts(string _tbName, string _ID, string _strCondition, int _Dist)
        
{            
            
//---存放取得查询结果总数的查询语句                    
            
//---对含有DISTINCT的查询进行SQL构造
            
//---对含有DISTINCT的总数查询进行SQL构造
            string strTmp="", SqlSelect="", SqlCounts="";
            
            
if (_Dist == 0)
            
{
                SqlSelect 
= "SELECT ";
                SqlCounts 
= "COUNT(*)";
            }

            
else
            
{
                SqlSelect 
= "SELECT DISTINCT ";
                SqlCounts 
= "COUNT(DISTINCT "+ _ID +")";
            }

            
if (_strCondition == string.Empty)
            
{
                strTmp 
= SqlSelect +" @Counts="+ SqlCounts +" FROM "+ _tbName;
            }

            
else
            
{
                strTmp 
= SqlSelect +" @Counts="+ SqlCounts +" FROM "+ " WHERE (1=1) "+ _strCondition; 
            }

            
return strTmp;
        }



        
/**//// <summary>
        
/// 获取分页数据查询SQL
        
/// </summary>
        
/// <param name="_tbName">----要显示的表或多个表的连接</param>
        
/// <param name="_fldName">----要显示的字段列表</param>
        
/// <param name="_PageSize">----每页显示的记录个数</param>
        
/// <param name="_Page">----要显示那一页的记录</param>
        
/// <param name="_PageCount">----查询结果分页后的总页数</param>
        
/// <param name="_Counts">----查询到的记录数</param>
        
/// <param name="_fldSort">----排序字段列表或条件(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')</param>
        
/// <param name="_Sort">----排序方法,0为升序,1为降序</param>
        
/// <param name="_strCondition">----查询条件,不需where</param>
        
/// <param name="_ID">----主表的主键</param>
        
/// <param name="_Dist">----是否添加查询字段的 DISTINCT 默认0不添加/1添加</param>
        
/// <returns></returns>                                                                           

        public static string getPageListSql(string _tbName, string _fldName, int _PageSize, int _Page, out int _PageCount, int _Counts, string _fldSort, int _Sort, string _strCondition, string _ID, int _Dist)
        
{                
            
string strTmp=""//---strTmp用于返回的SQL语句
            string SqlSelect="", strSortType="", strFSortType="";   

            
if (_Dist == 0)
            
{
                SqlSelect 
= "SELECT ";            
            }

            
else
            
{
                SqlSelect 
= "SELECT DISTINCT ";                
            }


            
if (_Sort == 0)
            
{
                strFSortType 
= " ASC";
                strSortType 
= " DESC";
            }

            
else
            
{
                strFSortType 
= " DESC";
                strSortType 
= " ASC";
            }


//            ----取得查询结果总数量-----
            int tmpCounts = 1;
            
if (_Counts != 0)
            
{
                tmpCounts 
= _Counts;
            }

//          --取得分页总数
            _PageCount = (tmpCounts + _PageSize - 1)/_PageSize;
            
//    /**//**当前页大于总页数 取最后一页**/
            if (_Page > _PageCount)
            
{
                _Page 
= _PageCount; 
            }

            
if (_Page <= 0)
            
{
                _Page 
= 1;
            }

//          --/*-----数据分页2分处理-------*/
            int pageIndex = tmpCounts/_PageSize;
            
int lastCount = tmpCounts%_PageSize;
            
if (lastCount > 0)
            
{
                pageIndex 
= pageIndex + 1;
            }

            
else
            
{
                lastCount 
= _PageSize;
            }

            
if (_strCondition == string.Empty) // --没有设置显示条件
            {
                
if (pageIndex < 2 || _Page <= (pageIndex/2 + pageIndex%2))  //--前半部分数据处理
                {
                    
if (_Page == 1)
                    
{
                        strTmp 
= SqlSelect +" TOP "+ _PageSize +" "+ _fldName +" FROM "+ _tbName +" ORDER BY "+ _fldSort +" "+ strFSortType;
                    }

                    
else
                    
{
                        strTmp 
= SqlSelect +" TOP "+ _PageSize +" "+ _fldName +" FROM "+ _tbName +" WHERE "+ _ID +" <(SELECT MIN("+ _ID +") FROM ("+ SqlSelect +" TOP "+ _PageSize*(_Page-1+" "+ _ID +" FROM "+ _tbName +
                            
" ORDER BY "+ _fldSort +" "+ strFSortType +") AS TBMinID) ORDER BY "+ _fldSort +" "+ strFSortType;
                    }

                }

                
else
                
{
                    _Page 
= pageIndex - _Page + 1//后半部分数据处理
                    if (_Page <= 1//--最后一页数据显示
                    {
                        strTmp 
= SqlSelect +" * FROM ("+ SqlSelect +" TOP "+ lastCount +" "+ _fldName +" FROM "+ _tbName +" ORDER BY "+ _fldSort +" "+ strSortType +") AS TempTB"+ " ORDER BY "+ _fldSort +" "+ strFSortType;
                    }

                    
else
                    
{
                        strTmp 
= SqlSelect +" * FROM ("+ SqlSelect +" TOP "+ _PageSize +" "+ _fldName +" FROM "+ _tbName +
                            
" WHERE "+ _ID +" >(SELECT MAX("+ _ID +") FROM("+ SqlSelect +" TOP "+ (_PageSize*(_Page-2)+lastCount) +" "+ _ID +" FROM "+ _tbName +
                            
" ORDER BY "+ _fldSort +" "+ strSortType +") AS TBMaxID) ORDER BY "+ _fldSort +" "+ strSortType +") AS TempTB ORDER BY "+ _fldSort +" "+ strFSortType;
                    }

                }

            }

            
else // --有查询条件
            {
                
if (pageIndex < 2 || _Page <=(pageIndex/2 + pageIndex%2))//--前半部分数据处理
                {
                    
if (_Page == 1)
                    
{
                        strTmp 
= SqlSelect +" TOP "+ _PageSize +" "+ _fldName +" FROM "+ _tbName +"WHERE 1=1 "+ _strCondition +" ORDER BY "+ _fldSort +" "+ strFSortType;
                    }

                    
else
                    
{
                        strTmp 
= SqlSelect +" TOP "+ _PageSize +" "+ _fldName +" FROM "+ _tbName +
                            
" WHERE "+ _ID +" <(SELECT MIN("+ _ID +") FROM ("+ SqlSelect +" TOP "+ (_PageSize*(_Page-1)) +" "+ _ID +" FROM " +_tbName +
                            
" WHERE 1=1 "+ _strCondition +" ORDER BY "+ _fldSort +" "+ strFSortType +") AS TBMaxID) "+ _strCondition +
                            
" ORDER BY "+ _fldSort +" "+ strFSortType;                            
                    }

                }

                
else //--后半部分数据处理
                {
                    _Page 
= pageIndex-_Page+1;
                    
if (_Page <= 1//--最后一页数据显示
                    {
                        strTmp 
= SqlSelect +" * FROM ("+ SqlSelect +" TOP "+ lastCount +" "+ _fldName +" FROM "+ _tbName +
                            
" WHERE 1=1 "+ _strCondition +" ORDER BY "+ _fldSort +" "+ strSortType +") AS TempTB ORDER BY "+ _fldSort +" "+ strFSortType;
                    }

                    
else
                    
{   
                        strTmp 
= SqlSelect +" * FROM ("+ SqlSelect +" TOP "+ _PageSize +" "+ _fldName +" FROM "+ _tbName +
                            
" WHERE "+ _ID +" >(SELECT MAX("+ _ID +") FROM("+ SqlSelect +" TOP "+ (_PageSize*(_Page-2)+ lastCount) +" "+ _ID +" FROM "+ _tbName +
                            
" WHERE 1=1 "+ _strCondition +" ORDER BY "+ _fldSort +" "+ strSortType +") AS TBMaxID) "+ _strCondition +
                            
" ORDER BY "+ _fldSort +" "+ strSortType +") AS TempTB ORDER BY "+ _fldSort +" "+ strFSortType;
                    }

                }

            }


            
return strTmp;
        }

    }

}

--以上代码是针对之前写的TOP MAX模式的分页存储过程修改
--以上分页算法对SQL SERVER 和 ACCESS同样有效
参见:http:
//www.cnblogs.com/hertcloud/archive/2005/12/21/301327.html