◆勇敢者论坛源代码分析系列(3)—分页查询
2008-12-28 16:05
295 查看
前言:勇敢者论坛是一个开放源码的ASP.NET c#论坛,使用VS.NET 2005开发管理,可以支持两种数据库 SQL2000或Accsee。作者的开发本意,就是提供给大家学习使用的。写本系列代码分析,也是为了初学者更快的入门和进步。祝大家学习愉快!多多指教。
勇敢者论坛: 程序演示 源码下载
前两节我们讲了简单的数据查询和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);
}
}
实现分页的方法很多,这里仅提供了这种供大家参考,记得吴旗娃给提供了一个专门分页的组件也很棒,有空大家可以研究研究。总之,个人认为,作什么都是实用为主,只要能够满足需求,就是好的方法,正所谓“不管黑猫白猫,抓住耗子就是好猫”,没有最好,只有更好,希望大家能够多注重实用(个人观点,欢迎拍砖)。
先就说这些把,自己做做测试把。
--------------------------------------------------------------
有什么不明确的,可以提出来讨论。高手和有写作经验的朋友,欢迎指点!
勇敢者论坛: 程序演示 源码下载
前两节我们讲了简单的数据查询和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);
}
}
实现分页的方法很多,这里仅提供了这种供大家参考,记得吴旗娃给提供了一个专门分页的组件也很棒,有空大家可以研究研究。总之,个人认为,作什么都是实用为主,只要能够满足需求,就是好的方法,正所谓“不管黑猫白猫,抓住耗子就是好猫”,没有最好,只有更好,希望大家能够多注重实用(个人观点,欢迎拍砖)。
先就说这些把,自己做做测试把。
--------------------------------------------------------------
有什么不明确的,可以提出来讨论。高手和有写作经验的朋友,欢迎指点!
相关文章推荐
- ◆勇敢者论坛源代码分析系列(1)—数据查询
- 勇敢者论坛源代码分析系列(2)—url参数传递
- Hibernate实现分页查询的原理分析
- TPC-H系列---7---TPC-H的22条查询语句分析(Q13--Q15)
- lightning mdb 源代码分析系列(3)
- Discuz!源代码分析系列:./include/global.func.php(4)
- Mysql系列(十三)分页查询语句
- hsqldb源码分析系列5 查询引擎之查询操作
- oracle分页查询的效率分析
- Hibernate实现分页查询的原理分析
- BlogEngine.Net架构与源代码分析系列part2:业务对象——共同的父类BusinessBase
- BlogEngine.Net架构与源代码分析系列part4:Blog全局设置——BlogSettings
- Hibernate实现分页查询的原理分析
- 淘淘商城系列——查询商品详情添加缓存分析
- MyBatis架构设计及源代码分析系列(一):MyBatis架构
- Sql Server系列:查询分页语句
- [导入]BlogEngine.Net架构与源代码分析系列part2:业务对象——共同的父类BusinessBase
- BlogEngine.Net架构与源代码分析系列part6:开放API——MetaWeblog与BlogImporter
- BlogEngine.Net架构与源代码分析系列part15:总结篇
- FBReader源代码分析系列一配置