当前位置:编程学习 > asp >>

重学>ASP.NET小实例>DataReader手工分页

花了两天时间,参考别人写好,

首先表数据如下:

\
 

直接上代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class learnFenye : System.Web.UI.Page
{
    SqlConnection sqlconn;
    protected void Page_Load(object sender, EventArgs e)
    {
        int PageSize = 3;//每页显示条数
        int PageCount = 0;//总共页数
        int RecordCount = 0;//数据库记录条数
        int CurrentPage = 0;//当前页数

       


        string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        sqlconn = new SqlConnection(conn);
        sqlconn.Open();
        //第一步:首先计算数据库的记录条数
        //RecordCount=
        RecordCount = CountRecord();
        Response.Write("数据库的记录条数有:" + RecordCount + "<br />");

        //第二步: 计算页数
        PageCount = RecordCount / PageSize;
        if (RecordCount % PageSize > 0)//如果整除不了,要多出一页
        {
            PageCount = PageCount + 1;
        }
        Response.Write("分页的页数有:" + PageCount + "<br />");


        //第三步:获取当前页数
        if (Request.QueryString["page"] != null)
        {
            CurrentPage = Convert.ToInt32(Request.QueryString["page"]);
        }
        else
        {
            CurrentPage = 1;//如果没设置,就从第一页开始
        }
        Response.Write("当前页数是:" + CurrentPage + "<br />");


        //第四步: 开始分页
        string sql = "SELECT TOP (" + PageSize + ") TicketAutoId, value FROM Ticket ";
        sql += "where TicketAutoId not in (select top " + (CurrentPage - 1) * PageSize + " TicketAutoId from Ticket order by TicketAutoId ) ";
        sql += " ORDER BY TicketAutoId";
        SqlCommand sqlcmd = new SqlCommand(sql, sqlconn);
        SqlDataReader sqldr = sqlcmd.ExecuteReader();
        string HTMLTable;
        HTMLTable = "<table>";
        while(sqldr.Read())
        {
           
            HTMLTable += "<tr><td>"+sqldr.GetInt32(0).ToString()+"</td></tr>";  
            //Response.Write(sqldr.GetInt32(0));
        }
        HTMLTable += "</table>";
        Response.Write(HTMLTable);
        //sqldr.Close();

        //第五步:设置首页,上一页,下一页,末页按钮
        int pagePre, pageNext;
        pagePre = CurrentPage - 1;
        pageNext = CurrentPage + 1;
        string pageHtml;

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

        if (pageNext > PageCount)
        {
            pageHtml += " 下页 末页";
        }
        else
        {
            pageHtml += " <a href=learnfenye.aspx?page=" + pageNext + ">下页</a>";
            pageHtml += " <a href=learnfenye.aspx?page=" + PageCount + ">末页</a>";
        }
        IndexButton.InnerHtml = pageHtml;

    }

    private int CountRecord()//统计数据库表的记录条数
    {
        int count = 0;
        string sql = "select count(*) from Ticket";
        SqlCommand sqlcmd = new SqlCommand(sql, sqlconn);
        SqlDataReader sqldr = sqlcmd.ExecuteReader();
        if (sqldr.Read() != null)
        {
            count = sqldr.GetInt32(0);
        }
        sqldr.Close();
        return count;
    }
}

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,