关于手写datareader分页程序的一段代码,有点小错误,呵呵,20分表心意!!!!!
主代码是这些:
public partial class iframefordiscovery: System.Web.UI.Page
{
public string html;
public OleDbConnection conn;
int RecordCount, PageCount, CurrentPage, PageSize, startID, endID;
public void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
PageSize = 15;
RecordCount = CalculateRecord();
PageCount = RecordCount / PageSize;
if (RecordCount % PageSize > 0)
PageCount = PageCount + 1;
lblPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
CurrentPage = 0;
}
}
protected void tohtml()
{
string startIDt, endIDt;
startID = 0;
endID = 0;
startIDt = Request.Params[ "sid "];
endIDt = Request.Params[ "eid "];
string ConnStr = "select top " + PageSize + " * from discovery ";
**************问题出现在这里*********************************************
if (startIDt != null)
{
startID = Convert.ToInt32(startIDt);
if (startID > 0)
{
ConnStr += " where identifier < " + startID;
}
}
if (endIDt != null)
{
endID = Convert.ToInt32(endIDt);
if (endID > 0)
{
ConnStr += " where identifier> " + endID;
}
}
ConnStr += " order by identifier desc ";
************问题出现在这里***************************************
OleDbConnection conn = new OleDbConnection(ConfigurationSettings.AppSettings[ "data "] + Server.MapPath(ConfigurationSettings.AppSettings[ "path "]));
conn.Open();
OleDbCommand mycomm=new OleDbCommand(ConnStr,conn);
OleDbDataReader mydr=mycomm.ExecuteReader();
string html = " <table width= '492 ' height= '28 ' border= '0 ' cellpadding= '0 ' cellspacing= '0 ' bgcolor= '444444 '> ";
int i = 0;
while (mydr.Read())
{
if (i == 0) endID = Convert.ToInt32(mydr[0]);
html+= " <tr align= 'center ' valign= 'middle '> ";
html+= " <td width= '52 ' height= '24 ' align= 'center '> ";
html+= " <img src= 'pic/isvideoaudio.gif ' width= '14 ' height= '14 '> ";
html+= " <img src= 'pic/ispicture.gif ' width= '14 ' height= '14 '> ";
html+= " </td> ";
html+= " <td width= '325 ' align= 'left '> ";
html += " <A class= 'readercss ' href= 'http://mail.sina.com.cn '> " + gettitle(FormatString(mydr[ "title "].ToString()),15) + " </A> ";
html+= " </td> ";
html+= " <td width= '80 ' align= 'center '> " + Convert.ToDateTime(mydr[ "time "]).ToString( "d ") + " </td> ";
html+= " <td width= '35 ' align= 'center '> ";
html+= "4343 ";
html+= " </td> ";
html+= " </tr> ";
i++;
if (i == PageSize) startID = Convert.ToInt32(mydr[0]);
}
mydr.Close();
html += " </table> ";
mblist.InnerHtml = html;
if (Request.Params[ "page "] != null)
CurrentPage = Convert.ToInt32(Request.Params[ "page "]);
if (PageCount == 0)
{
lblCurrentPage.Text = "0 ";
pagelist.InnerHtml = "上一页 下一页 ";
}
else
{
lblCurrentPage.Text = CurrentPage.ToString();
pagelist.InnerHtml = " <a href= 'iframefordiscovery.aspx?page= " + (CurrentPage - 1) + "&eid= " + endID + " '> 上一页 </a> <a href= 'iframefordiscovery.aspx?page= " + (CurrentPage + 1) + "&sid= " + startID + " '> 下一页 </a> ";
if (CurrentPage == PageCount)
pagelist.InnerHtml = " <a href= 'iframefordiscovery.aspx?page= " + (CurrentPage - 1) + "&eid= " + endID + " '> 上一页 </a> 下一页 ";
if (CurrentPage == 0)
{
pagelist.InnerHtml = "上一页 <a href= 'iframefordiscovery.aspx?page= " + (CurrentPage + 2) + "&sid= " + startID + " '> 下一页 </a> ";
lblCurrentPage.Text = (CurrentPage + 1).ToString();
}
}
}
public int CalculateRecord()
{
int intCount;
OleDbConnection conn = new OleDbConnection(ConfigurationSettings.AppSettings[ "data "] + Server.MapPath(ConfigurationSettings.AppSettings[ "path "]));
conn.Open();
string sql= "select count(*) from discovery ";
OleDbCommand comm=new OleDbCommand(sql,conn);
OleDbDataReader dr=comm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr[0].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
打个比方说:共有47条记录,分为4页,每页也就是pagesize=15,这样最后一页只有2条记录,也就是identifier为1和2.这时候我点前一页的时候会跳到从47到32这些记录,请问我该如何写sql语句做这个限制啊?小弟不才,还请指点,20分小送......
ps:都是给人家打工,不容易的,就指点下吧~
[解决办法]
看了一会 看不太明白lz的意思,
现在能实现什么???
**************问题出现在这里*********************************************
if (startIDt != null)
{
startID = Convert.ToInt32(startIDt);
if (startID > 0)
{
ConnStr += " where identifier < " + startID;
}
}
if (endIDt != null)
{
endID = Convert.ToInt32(endIDt);
if (endID > 0)
{
ConnStr += " where identifier> " + endID;
}
}
ConnStr += " order by identifier desc ";
************问题出现在这里***************************************
这段代码有问题,sql语句会出现两个where
if (startIDt != null && endIDt != null)
{
startID = Convert.ToInt32(startIDt);
if (startID > 0)
{
ConnStr += " where identifier < " + startID;
}
endID = Convert.ToInt32(endIDt);
if (endID > 0)
{
ConnStr += " and identifier> " + endID;
}
}
ConnStr += " order by identifier desc ";