ASP.NET极速DataReader分页程序

    前段时间因工作需要写了段ASP.NET手工分页程序,几经修改优化,或许因本人知识有限,觉得其性能已经不能再提高多少。故将之发布上来与大家分享,欢迎大家提出更进一步的优化方法。
                                                         [Gerry] 2006-5-9

<%@ Page Language="C#" Debug="true" %>

<%@Import Namespace="System.Data"%>
<
%@Import Namespace="System.Data.SqlClient"%>

<Script Language="C#" runat="server">

String PageName="wznPage"; //配置本页面的页面名字与本文件名相同
String TableName="Ticket"; //配置要查询的数据库表的名字
String IndexID="TicketAutoId"; //配置数据库表的主索引排序ID
String ConnString="server=localhost;uid=sa;pwd=;database=qq3"; //配置数据库连接字符串
int PageSize=7; //配置每页显示记录数
SqlConnection conn; //定义连接数据库全局变量

void Page_Load(Object src,EventArgs e)
{
    conn =new SqlConnection(ConnString);
    conn.Open(); //建立与数据库的连接

    int RecordCount=0; //总共记录数
    int PageCount=0;   //总共页数
    int CurrentPage=0; //当前页码
   
    RecordCount = CountRecord();  //计算总共有多少记录

    PageCount = RecordCount/PageSize; //计算总共有多少页 取整
    if (RecordCount%PageSize > 0) {
        PageCount = PageCount + 1;    //如果不整除则加一页来显示除后剩余记录
    }

    if (Request.QueryString["page"]!=null) {
        CurrentPage = Convert.ToInt32(Request.QueryString["page"]); //获得页的值
    }
    else {
        CurrentPage = 1;
    }

    //核心sql语句部分
    string sql;
    sql="select top "+PageSize+" * from "+ TableName +" where "+ IndexID +" not in (";
    sql+=" select top "+((CurrentPage-1)*PageSize)+" "+ IndexID +" from "+ TableName +" order by "+ IndexID +"";
    sql+=" ) order by "+ IndexID +"";
    //Response.Write("sql:"+sql);

    SqlCommand cmd=new SqlCommand(sql,conn);
    SqlDataReader myReader = cmd.ExecuteReader();

    string mainHtml="<table border='0' cellpadding='0' cellspacing='0' width='100%' height='139'>";
    int i=1;
    while(myReader.Read())
    {
        mainHtml+="<tr><td width='20%'></td><td>本页第"+ i +"条数据:</td></tr>";
        mainHtml+="<tr><td></td><td>数据表第1个字段的值是:"+myReader[0].ToString().Trim()+"</td></tr>";
        mainHtml+="<tr><td></td><td>数据表第2个字段的值是:"+myReader[1].ToString().Trim()+"</td></tr>";
        mainHtml+="<tr><td></td><td>数据表第3个字段的值是:"+myReader[2].ToString().Trim()+"</td></tr>";
        mainHtml+="<tr><td></td><td><hr></td></tr>";

        i++;
    }
    mainHtml+="</table>";
    myReader.Close();

    int pagePre,pageNext;
    pagePre=CurrentPage-1;
    pageNext=CurrentPage+1;
    string pageHtml;

    if (pagePre==0) {
        pageHtml="首页&nbsp;上页";
    }
    else {
        pageHtml="<a href='"+ PageName +".aspx?page=1'>首页</a>&nbsp;<a href='"+ PageName +".aspx?page="+pagePre+"'>上页</a>";
    }

    if (pageNext>PageCount) {
        pageHtml+="&nbsp;下页&nbsp;末页";
    }
    else {
        pageHtml+="&nbsp;<a href='"+ PageName +".aspx?page="+ pageNext +"'>下页</a>";
        pageHtml+="&nbsp;<a href='"+ PageName +".aspx?page="+ PageCount +"'>末页</a>";
    }

    mblist.InnerHtml = mainHtml;                 //给分页主内容标签赋值
    lblPageCount.Text = PageCount.ToString();    //给总页数标签赋值
    lblRecordCount.Text = RecordCount.ToString();//给总记录数标签赋值
    lblCurrentPage.Text = CurrentPage.ToString();//给当前页码标签赋值
    pagelist.InnerHtml = pageHtml;               //给翻页链接标签赋值
}


//统计数据库表中记录总数的函数
public int CountRecord()
{
    int intCount = 0;
    string sql = "select count(*) from "+ TableName;
    SqlCommand cmd = new SqlCommand(sql,conn);
    SqlDataReader myReader = cmd.ExecuteReader();
    if(myReader.Read()) {
        intCount = Int32.Parse(myReader[0].ToString());
    }
    myReader.Close();
    return intCount;
}
</script>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>#</title>
<style type="text/css">
<!--
td {
    font-family: "宋体";
    font-size: 14px;
}
a:link {
    text-decoration: none;
    color: blue;
}
a:visited {
    text-decoration: none;
    color: blue;
}
a:hover {
    text-decoration: underline;
    color: blue;
}
a:active {
    text-decoration: none;
    color: blue;
}
-->
</style>
</head>
<body topmargin="0" leftmargin="0">
<form runat=server>
  <table width="778" border="1" cellpadding="0" cellspacing="0" bgcolor="#e2e2e2" align="center">
    <tr>
      <td width="562" height="400" align="center" valign="top"><br>
        <!-- 数据显示位置 -->
        <span id="mblist" runat="server"></span>
        <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
          <tr>
            <td align="center">共有
              <asp:Label id="lblRecordCount" ForeColor="red" runat="server" />
              条记录 当前为
              <asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />
              /
              <asp:Label id="lblPageCount" ForeColor="red" runat="server" />
              页 </td>
            <td align=center><!-- 分页显示位置 -->
              <span id="pagelist" runat="server"></span> </td>
          </tr>
        </table></td>
    </tr>
  </table>
</form>
</body>
</html>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值