分享
 
 
 

将以前写的SQL2分查找法通用分页存储过程算法 改成.net类实现

王朝c#·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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

//调用函数例子

public IList getParkDataList(string key, int curPage, out int pageCount, int pageSize, int Counts)

...{

IList list = new ArrayList();

string SECLECT_FIELD = "T_Park.ParkID, T_Park.ParkTitle, T_Park.ParkLetter, T_ParkArea.AreaName, T_ParkType.ParkTypeName ";

string SECLECT_TABLE = "T_ParkType INNER JOIN (T_ParkArea INNER JOIN T_Park ON T_ParkArea.ParkAreaID = T_Park.ParkAreaID) ON T_ParkType.ParkTypeID = T_Park.ParkTypeID";

string SECLECT_CONDITION = string.Empty;

if (key != string.Empty)

...{

SECLECT_CONDITION = " AND T_Park.ParkTitle like '%"+ key +"%'";

}

string SELECT_ID = "ParkID";

string SELECT_FLDSORT = "ParkID";

int SELECT_SORT = 1;

int SELECT_DIST = 0;

string SQL = PageList.getPageListSql(SECLECT_TABLE, SECLECT_FIELD, pageSize, curPage, out pageCount, Counts, SELECT_FLDSORT, SELECT_SORT, SECLECT_CONDITION, SELECT_ID, SELECT_DIST);

//string strCondition;

OleDb db = new OleDb();

ParkBE park;

using(OleDbDataReader dr = (OleDbDataReader)db.ExecuteReader(CommonFun.GetConnectionString(), CommandType.Text, SQL))

...{

while (dr.Read())

...{

park = new ParkBE();

park.ParkID = Convert.ToInt32(dr[0]);

park.ParkTitle = dr[1].ToString();

park.ParkLetter = dr[2].ToString();

park.ParkAreaName = dr[3].ToString();

park.ParkTypeName = dr[4].ToString();

list.Add(park);

}

}

return list;

}

http://blog.csdn.net/todaywlq/archive/2007/01/29/1497418.aspx

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有