◆勇敢者论坛源代码分析系列(3)—分页查询
来源:互联网 发布:php抓取新闻 编辑:程序博客网 时间:2024/04/30 01:07
勇敢者论坛: 程序演示 源码下载
前两节我们讲了简单的数据查询和url参数传递,这节我们综合上述两种技巧,实现以下分页查询。
以勇敢者论坛(开放源码的ASP.NET论坛) 的 Mainlist.aspx(就是各个分类的帖子列表的页面)为例,看看具体是怎么实现的。
在Mainlist.aspx中,找到<asp:datalist id="DL_Main" runat="server" Width="100%">,这就是我们显示列表用的组件。在代码的BindGrid()函数中是实现的主要代码,我把重点的部分弄出来,标示以下
public void BindGrid()
{
。。。
int BID,SID,solved;
if ((Request.QueryString["BID"]==null)||(Request.QueryString["SID"]==null))
{
BID=1;
SID=200;
}
else
{
BID=System.Convert.ToInt32(Request.QueryString["BID"]);
SID=System.Convert.ToInt32(Request.QueryString["SID"]);
}
。。。
PageSize=System.Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["MainList"]);
CurrentPage = 0;
//计算总共有多少记录
int RecordCount = DBOperate.CalculateQRecord(BID, SID,solved);
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
PageCount = (RecordCount+PageSize-1)/PageSize;
if (PageCount<=0)
PageCount=1;
lblPageCount.Text = PageCount.ToString();
//获取页面参数
if (Request.QueryString["Page"] != null)
CurrentPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurrentPage=1;
if (PageCount<CurrentPage)
CurrentPage=PageCount;
if (CurrentPage<=0)
CurrentPage=1;
//ListBind();
//设定导入的起终地址
int StartIndex = (CurrentPage-1)*PageSize;
this.DL_Main.DataSource = DBOperate.SelectFormForum("SELECT Q_ID, author,Questions.Q_Note, Q_content, ImageName, HiteCounter, ReleaseTime,ReferTime, BigClass_ID, SmallClass_ID, AnsCounter, kind, solved FROM Questions WHERE (SmallClass_ID = " + SID + ") AND (kind > 0) AND (kind < 5) AND (solved =" + solved + ") ORDER BY kind DESC, ReleaseTime DESC", StartIndex, PageSize, "Questions");
this.Page.DataBind();
lnkNext.Enabled = true;
lnkPrev.Enabled = true;
if(CurrentPage==(PageCount))
{
lnkNext.Enabled = false;
}
else
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?solved="+solved+"&BID="+ BID.ToString()+"&SID="+ SID.ToString()+"&Page=" + Convert.ToString(CurrentPage+1);
if(CurrentPage==1)
lnkPrev.Enabled = false;
else
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?solved="+solved+"&BID="+ BID.ToString()+"&SID="+ SID.ToString()+"&Page=" + Convert.ToString(CurrentPage-1);
lblCurrentPage.Text = CurrentPage.ToString();
。。。
}
其中,最重点的是: this.DL_Main.DataSource = DBOperate.SelectFormForum("SELECT Q_ID, author,Questions.Q_Note, Q_content, ImageName, HiteCounter, ReleaseTime,ReferTime, BigClass_ID, SmallClass_ID, AnsCounter, kind, solved FROM Questions WHERE (SmallClass_ID = " + SID + ") AND (kind > 0) AND (kind < 5) AND (solved =" + solved + ") ORDER BY kind DESC, ReleaseTime DESC", StartIndex, PageSize, "Questions");
DBOperate.SelectFormForum这个函数在App_Code/DBOperate.cs中,
public static DataTable SelectFormForum(string sql,int start,int size,string tablename)
{
try
{
//读数据库类型
string DBtype = ConfigurationManager.AppSettings["DBtype"];
if (DBtype == "Accsee")
{
//Accsee 数据库查询操作
DBsql exsql = new DBsql();
exsql.Open();
OleDbDataAdapter da = new OleDbDataAdapter(sql, exsql.con);
DataSet ds = new DataSet();
da.Fill(ds, start, size, tablename);
exsql.Close();
return ds.Tables[0];
}
else
{
//sql server 数据库查询操作
SqlConnection sqlConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DBConnection"]);
SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection);
DataSet returns = new DataSet();
sqlAdapter1.Fill(returns, start, size, tablename);
return returns.Tables[0];
}
}
catch (Exception ex)
{
throw (ex);
}
}
实现分页的方法很多,这里仅提供了这种供大家参考,记得吴旗娃给提供了一个专门分页的组件也很棒,有空大家可以研究研究。总之,个人认为,作什么都是实用为主,只要能够满足需求,就是好的方法,正所谓“不管黑猫白猫,抓住耗子就是好猫”,没有最好,只有更好,希望大家能够多注重实用(个人观点,欢迎拍砖)。
先就说这些把,自己做做测试把。
--------------------------------------------------------------
有什么不明确的,可以提出来讨论。高手和有写作经验的朋友,欢迎指点!
- ◆勇敢者论坛源代码分析系列(3)—分页查询
- ◆勇敢者论坛源代码分析系列(1)—数据查询
- 勇敢者论坛源代码分析系列(2)—url参数传递
- 勇敢者论坛完整源代码(asp.net c# SQL 2000)发布--asp.net开源论坛代码
- 《分页》分析及 源代码分析
- lightning mdb 源代码分析系列(3)
- Oracle分页查询分析
- 勇敢者的游戏——VC
- SQL查询分页分页(SQL语法系列)
- MSSql Server基础学习系列———分页查询
- WebKit 内核源代码分析系列
- redis源代码分析系列文章
- 查询分页技术实现分析
- SQL分页查询语句分析
- sQL分页查询语句分析
- SQL分页查询语句分析
- Sql 分页查询效率分析
- MySQL 分页查询性能分析
- ◆勇敢者论坛源代码分析系列(1)—数据查询
- 在eclipse中下载插件及插件安装的方法(zz)
- 勇敢者论坛源代码分析系列(2)—url参数传递
- 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
- Axure RP Pro - Download下载 - Axure RP Pro 5.0.0.1515
- ◆勇敢者论坛源代码分析系列(3)—分页查询
- 当下10大最热门的网站开发技术
- 排序算法思想和实现(代码)(二)
- IBM WebSphere Application Server V7.0 中的 Web Services 安全策略及配置
- linux c编程 “stdio.h:没有该文件或目录”
- 程序员的10个等级
- test
- 静态链接库与动态链接库总结
- oracle 事务