Sqlserver数据库分页的实际运用

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.Text;

namespace NetWork.Management.Admin.Zhuanti
{
    public partial class fenye : System.Web.UI.Page
    {

        private int PageIndex = 0;//当前页码
        private int PageSize = 10;//每页几条记录
        private int Total = 499;//总记录
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParams();
                DataSet ds = PageData(PageSize, PageIndex, Total);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                lbl_page.Text = GetDivPager("", ds);
            }
        }

        //数据库连接字符
        public static string StrConn()
        {
            string ConStr = "server=.;database=fenye;user=sa;password=123";
            return ConStr;
        }
        //Get方式获得下一页
        private void GetParams()
        {
            if (!String.IsNullOrEmpty(Request["page"]))
            {
                PageIndex = Convert.ToInt32(Request["Page"]);
            }
            else
            {
                PageIndex = 1;
            }
        }

        #region 获得分页字符
        public string GetDivPager(string queryString, DataSet ds)
        {
            StringBuilder sp = new StringBuilder();
            int TotalCount = Total;
            int rowCount = Total/PageSize;
            if (ds != null)
            {
                sp.AppendFormat("  <p>总记录:<span id=\"sum\">{0}</span>", TotalCount);
                sp.AppendFormat("  页码:<em><b id=\"current\">{0}</b>/<span id=\"count\">{1}</span></em> ", PageIndex, rowCount);
                sp.AppendFormat("  每页:<span id=\"eachPage\">{0}</span></p> ", PageSize);

                sp.AppendFormat("  <a  href='{0}'>首页</a> ", "?page=1" + queryString);
                if (PageIndex > 1)
                {
                    sp.AppendFormat("  <a href='{0}'>< 上一页 </a>", "?page=" + (PageIndex - 1) + queryString);
                }
                int temp = 0;
                int loopc = rowCount > 10 ? 10 : rowCount;
                for (int i = 0; i < loopc; i++)
                {
                    temp = i + 1;
                    if (PageIndex > 10) { temp = (PageIndex - 10) + i + 1; }
                    sp.AppendFormat("  <a class=\"{0}\" href='{1}'>{2}</a>", PageIndex == temp ? "active" : "", "?page=" + temp + queryString, temp);
                }
                if (PageIndex != rowCount)
                {
                    sp.AppendFormat("  <a href='{0}'>下一页 ></a>", "?page=" + (PageIndex + 1) + queryString);
                }
                sp.AppendFormat("  <a href='{0}'>尾页</a>", "?page=" + rowCount + queryString);

            }
            else
            {
                ds = null;
            }
            return sp.ToString();
        }
        #endregion


        #region 获取分页的数据
        /// <summary>
        /// 获取分页的数据
        /// </summary>
        /// <param name="PageSize">每页显示多少条数据</param>
        /// <param name="pageIndex">当前页码</param>
        /// <returns></returns>
        public static DataSet PageData(int PageSize, int pageIndex, int Total)
        {

            SqlConnection conn = new SqlConnection(StrConn());
            SqlCommand comm = new SqlCommand("P_LoadPageData", conn);

            
            comm.Parameters.Add(new SqlParameter("@pageSize", SqlDbType.Int)).Value = PageSize;
            comm.Parameters.Add(new SqlParameter("@pageIndex", SqlDbType.Int)).Value = pageIndex;
            comm.Parameters.Add(new SqlParameter("@total", SqlDbType.Int)).Value = Total;


            comm.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);

            Total = (int)comm.Parameters["@total"].Value;

            conn.Close();
            conn.Dispose();
            comm.Dispose();

            return ds;
        }
        #endregion


    }
}

前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="fenye.aspx.cs" Inherits="NetWork.Management.Admin.Zhuanti.fenye" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
       <asp:Label ID="lbl_page" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值