首页 > Web开发 > 详细

ASP.NET分页存储过程

时间:2014-03-29 07:31:04      阅读:553      评论:0      收藏:0      [点我收藏+]

存储过程:

bubuko.com,布布扣
-- =============================================
-- Author:
-- Create date: 
-- Description:    分页
--Update Date:
--增加了默认排序规则,根据主键升序(防止在视图查询中乱号)
-- =============================================
ALTER PROCEDURE [dbo].[getdatabyPageIndex]
@tablename nvarchar(200),
@columns nvarchar(500)=*,
@condition nvarchar(200)=‘‘,
@pagesize int=10,
@pageindex int=0,
@pk nvarchar(30),
@total int output, --统计总共的条数
@orderculumn nvarchar(50)=@pk,
@isasc nvarchar(10)=desc

AS
BEGIN
    DECLARE @sql nvarchar(2000)
    SET @sql=select top +cast(@pagesize AS nvarchar(10))+ +@columns+ from +@tablename+ where +
    @pk+ not in (select top +cast((@pagesize*@pageindex) AS nvarchar(10))+
     +@pk+ from +@tablename + where 1=1 +@condition+ order by +@orderculumn+ +@isasc+)+@condition + order by +@orderculumn+ +@isasc
     PRINT @sql
        EXEC(@sql)
        DECLARE @sql2 nvarchar(2000)        
        SET  @sql2=SELECT @total1 = count(*) FROM + @tablename+ WHERE 1=1 + @condition
        EXEC sp_executesql @sql2,N@total1 int output,@total output
       
END
View Code

.cs:

bubuko.com,布布扣
      /// <summary>
      /// 分页功能
      /// </summary>
      /// <param name="tablename">表名</param>
      /// <param name="columns">列名</param>
      /// <param name="condition">条件,不需要带where</param>
      /// <param name="pagesize">每页显示条数</param>
      /// <param name="pageindex">页码</param>
      /// <param name="pk">主键</param>
      /// <returns>DataTable</returns>
      public DataTable getdatabyPageIndex(string tablename, string columns, string condition, int pagesize, int pageindex, string pk,out int totalcount,string ordercolumn,string isasc)
      {
          string order = "";
          if (ordercolumn == null)
          {
              order = pk;
          }

          string asc = "";
          if (isasc == null)
          {
              isasc = "desc";
          }

        SqlParameter[] pars = new SqlParameter[]{
         new SqlParameter("@tablename",tablename),
         new SqlParameter("@columns",columns),
         new SqlParameter("@condition",condition),
         new SqlParameter("@pagesize",pagesize),
         new SqlParameter("@pageindex",pageindex),
         new SqlParameter("@pk",pk),
         new SqlParameter("@total",SqlDbType.Int),
         new SqlParameter("@orderculumn",ordercolumn),
         new SqlParameter("@isasc",isasc)
       };
         pars[6].Direction = ParameterDirection.Output;
         DataTable dt= db.ExcuteSelectReturnDataTable("sp_getdatabyPageIndex", CommandType.StoredProcedure, pars);
         totalcount=  int.Parse(pars[6].Value.ToString());
         return dt;       
      }


        /// <summary>
        /// 执行一个Select语句或者相应的存储过程实现返回数据集合DataSet
        /// </summary>
        /// <param name="SelectStr">执行一个Select语句或者相应的存储过程</param>
        /// <param name="type">指定命令类型</param>
        /// <param name="pars">相应参数集合</param>
        /// <returns>DataSet</returns>
        public DataSet ExcuteSelectReturnDataSet(string SelectStr, CommandType type, SqlParameter[] pars)
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(ConnString);            
            SqlDataAdapter sda = new SqlDataAdapter(SelectStr, conn);
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter p in pars)
                {
                    sda.SelectCommand.Parameters.Add(p);
                }
            }
            sda.SelectCommand.CommandType = type;
            sda.Fill(ds);
            return ds;        
        }
View Code

 .aspx:

bubuko.com,布布扣
    <style type="text/css">
        .pages {  color: #999; }
        .pages a, .pages .cpb { text-decoration:none;float: left; padding: 0 5px; border: 1px solid #ddd;background: #ffff;margin:0 2px; font-size:11px; color:#000;}
        .pages a:hover { background-color: #2F7EAE; color:#fff;border:1px solid #2F7EAE; text-decoration:none;}
        .pages .cpb { font-weight: bold; color: #fff; background: #2F7EAE; border:1px solid #2F7EAE;}
        .bt{ padding-left:10px}
    </style>


<webdiyer:AspNetPager ID="AspNetPager1" runat="server" FirstPageText="首页"
                            LastPageText="尾页" NextPageText="下一页" PageIndexBoxType="DropDownList"
                            PrevPageText="上一页" ShowCustomInfoSection="Left" ShowPageIndexBox="Always"
                            SubmitButtonText="Go" TextAfterPageIndexBox="" TextBeforePageIndexBox="转到"
                            AlwaysShow="True"
                            CustomInfoHTML="第%CurrentPageIndex%/%PageCount%页,每页%PageSize%条,共%RecordCount%条信息&nbsp;"
                            LayoutType="Table" CssClass="pages" CurrentPageButtonClass="cpb" CustomInfoClass="paginator"
                            Height="20px" Wrap="False"
                            NumericButtonCount="2"
                            CustomInfoSectionWidth="60%" OnPageChanging="AspNetPager1_PageChanging"
                            OnPageChanged="AspNetPager1_PageChanged">
                        </webdiyer:AspNetPager>
View Code

.aspx.cs:

bubuko.com,布布扣
    string condition = "  ";
    //每页条数
    int pagesize = 1;
    //总共条数
    int recordCount = 0;
    //第几页
    int pageindex = 0;


    public void getInfo()
    { 
        rptwhs.DataSource = db.getdatabyPageIndex("tbweihushang","*",condition,pagesize,pageindex,"id",out recordCount,null,null);
        rptwhs.DataBind();
        AspNetPager1.RecordCount = recordCount;
        AspNetPager1.PageSize = pagesize;
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        pageindex = AspNetPager1.CurrentPageIndex - 1;
        getInfo();
    }
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        getInfo();
    }
View Code

ASP.NET分页存储过程,布布扣,bubuko.com

ASP.NET分页存储过程

原文:http://www.cnblogs.com/liuswi/p/3632016.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!