最新的c#分页办法

18 篇文章 0 订阅

1 建议基本分页方法的基础->sql语句函数


 /// <summary>
        ///产生分页必须的sql语句的方法
        /// </summary>
        /// <param name="tabName">表名称</param>
        /// <param name="fieldName">查询的字段名</param>
        /// <param name="fldName">主键名</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="strWhere">增加条件语句</param>
        /// <param name="orderType">排序类别 TRUE 的话是降序排列</param>
        /// <returns></returns>
        public static string Asppage(string tabName, string fieldName, string fldName,string ordername, int pageSize, int pageIndex, string strWhere, bool orderTyp)
        {
            StringBuilder sbSql = new StringBuilder();
            string strTemp = "";
            string strOrder = "";
            if (orderTyp)
            {
                strOrder = " order by " + ordername + " desc";
                strTemp = " not in (select top " + (pageIndex - 1) * pageSize + " " + fldName + " from " + tabName + strOrder + ")";
            }
            else
            {
                strOrder = " order by " + ordername + " asc";
                strTemp = " not in (select top " + (pageIndex - 1) * pageSize + " " + fldName + " from " + tabName + strOrder + ")";
            }
            if (pageIndex == 1)
            {
                strTemp = "";
                if (!string.IsNullOrEmpty(strWhere.Trim()))
                {
                    strTemp = " WHERE " + strWhere;
                }
                sbSql.Append("select top " + pageSize + " " + fieldName + " FROM " + tabName + " " + strTemp + " " + strOrder);
            }
            else
            {
                if (string.IsNullOrEmpty(strWhere.Trim()))
                {
                    sbSql.Append("select top " + pageSize + " " + fieldName + " from " + tabName + " where " + fldName + strTemp + strOrder);

                }
                else
                {
                    strTemp = " not in (select top " + (pageIndex - 1) * pageSize + " " + fldName + " from " + tabName + " where " + strWhere + strOrder + ")";
                    sbSql.Append("select top " + pageSize + " " + fieldName + " from " + tabName + " where " + fldName + strTemp + " and " + strWhere + strOrder);
                }
            }
            return sbSql.ToString();
        }


-----------------------------------------------------------------

2 调用上面产生的sql 语句,返回数据datable的方法

  /// <summary>
        /// 返回页面的信息,全部按降序排列
        /// </summary>
        /// <param name="everypagenuber">每页分页数量</param>
        /// <param name="nubcount">返回总数量</param>
        /// <param name="pagecount">返回总页数</param>
        /// <param name="indexpage">输入当前页码</param>
        /// <param name="strwhere">条件语句</param>
        /// <param name="strorderby">排序条件</param>
        /// <param name="userid">当前用户id</param>
        /// <returns>返回内容列表</returns>
        public DataTable pagedate(int everypagenuber, ref Decimal nubcount,ref Decimal pagecount,int indexpage,string strwhere,string strorderby)
        {
           
            string sql2 = " select count(1) from lc_order where " + strwhere;
            DataTable rs1 = DAL.SQLHelper.ExecuteDataTable(DAL.SQLHelper.CONN_STRING, CommandType.Text, sql2);
            nubcount = Decimal.Parse(rs1.Rows[0][0].ToString());//总数
            if(nubcount==0)
            {
                pagecount=0;
            }else
            {
                pagecount = Math.Ceiling(nubcount / everypagenuber);
            }
            string sql = DLL.Input.Asppage("lc_order", " * ", "id", strorderby, everypagenuber, indexpage, strwhere, true);

            DataTable rs = new DataTable();
            rs = DAL.SQLHelper.ExecuteDataTable(DAL.SQLHelper.CONN_STRING, CommandType.Text, sql);
            return rs;
        
        }

-----------------------------------------------------------

3. 在cs页面中如此使用

 DLL.lc_order mode = new DLL.lc_order();

            string sqlwhere ="  addtime between convert(datetime,'" + btime.Text.Trim() + "') and convert(datetime,'" + etime.Text.Trim() + "') and (order_status=0 and issoft=0 and sh=0) and userid=" + DLL.admins.getuserid();
            
           string page=Request.QueryString["page"];
            if(string.IsNullOrEmpty(page))
            {
             page="1";
            }

             Decimal nubcount=0,pagecount=0;
            rp_NewsList.DataSource=mode.pagedate(2,ref nubcount,ref pagecount,int.Parse(page),sqlwhere,"addtime");
            rp_NewsList.DataBind();

            Literal1.Text = DLL.Input.pagehtml(int.Parse(page),Convert.ToInt16(pagecount), 0);

-------------------------------------------------------

4. 在aspx页面中使用

  <asp:Repeater ID="rp_NewsList" runat="server">
    <ItemTemplate>

  </ItemTemplate>
    </asp:Repeater>
         
 <asp:Literal ID="Literal1" runat="server"></asp:Literal>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值