asp.net存储过程分页+GridView控件 几百万数据 超快

时间:2022-02-17 23:15:19

存储过程:---亲测275万数据,分页速度N快


create PROCEDURE PageList 
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int,--每页显示记录条数
@currentpage int,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列
@rowcount int output,--总记录数,共有几条信息
@pagecount int output--总页数,共有多少页
AS
declare @countsql nvarchar(50)
declare @sql nvarchar(200)
declare @subsql nvarchar(100)--not in子sql语句
declare @tmpOrderid nvarchar(50) 
--返回总记录数,并赋值给输出参数@rowcount
set @countsql='select @totalcount=count(*) from '+@tablename
exec sp_executesql @countsql,N'@totalcount int out',@rowcount output --判断字段名是否为空
if @fieldname is null or @fieldname=''
set @fieldname=' * ' --判断是否排序及排序方式
if @orderid is null or @orderid=''
set @tmpOrderid=' '
else
begin 
if @sort=0
set @tmpOrderid='order by '+@orderid+' desc'
else
set @tmpOrderid='order by '+@orderid+' asc'
end --计算页数
if @rowcount%@pagesize>0
set @pagecount =(@rowcount/@pagesize)+1;
else
set @pagecount=@rowcount/@pagesize; --分页算法实现
set @subsql='select top'+str(@pagesize*(@currentpage-1))+' '+@orderid+' from '+@tablename+' '+@tmpOrderid
set @sql='select top'+str(@pagesize)+' '+@fieldname+' from '+@tablename+' where '+@orderid+' not in ('+@subsql+')'+@tmpOrderid
exec(@sql)

aspx文件:


<asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        </div>
        第<asp:Label ID="lblCurrent" runat="server"></asp:Label>页 | 每页<asp:Label ID="lblPageSize"
            runat="server"></asp:Label>条 | 共有<asp:Label ID="lblPageTotal" runat="server"></asp:Label>页 |  共有<asp:Label ID="lblRowsTotal"
            runat="server"></asp:Label>条信息 |
        <asp:HyperLink ID="hlFirst" runat="server">首页</asp:HyperLink>
        |
        <asp:HyperLink ID="hlPrev" runat="server">上一页</asp:HyperLink>
        |
        <asp:HyperLink ID="hlNext" runat="server">下一页</asp:HyperLink>
        |
        <asp:HyperLink ID="hlLast" runat="server">尾页</asp:HyperLink><br />

.cs文件:


protected void Page_Load(object sender, EventArgs e)
    {
        int CurrentPage,RowCount,PageCount;
        int PageSize = ;
        if (Request["CurrentPage"] == null||Convert.ToInt32(Request["CurrentPage"])< )
        {
            CurrentPage=;
        }
        else
        {
            CurrentPage=Convert.ToInt32(Request["CurrentPage"]);
        }         //数据库操作
        SqlConnection sqlconn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"].ToString());
        SqlCommand cmd = new SqlCommand("pagelist", sqlconn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = sqlconn;
        SqlParameter[] prams ={
                               new SqlParameter("@tablename",SqlDbType.NVarChar,),
                               new SqlParameter("@fieldname",SqlDbType.NVarChar,),
                               new SqlParameter("@pagesize",SqlDbType.Int),
                               new SqlParameter("@currentpage",SqlDbType.Int),
                               new SqlParameter("@orderid",SqlDbType.NVarChar,),
                               new SqlParameter("@sort",SqlDbType.Int),
                               new SqlParameter("@rowcount",SqlDbType.Int),
                               new SqlParameter("@pagecount",SqlDbType.Int)};
        prams[].Value = "news";//表名
        prams[].Value ="*";//字段名
        prams[].Value =PageSize;//每页显示条数
        prams[].Value =CurrentPage;//当前页数
        prams[].Value ="id";//主键
        prams[].Value =;//排序方式,0表示降序,1表示升序
        prams[].Direction = ParameterDirection.Output;//总记录数
        prams[].Direction = ParameterDirection.Output;//总页数
        foreach(SqlParameter pram in prams)
        {
            cmd.Parameters.Add(pram);
        }
        sqlconn.Open();
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();
        sda.SelectCommand = cmd;
        sqlconn.Close();
        sda.Fill(ds);
        //数据库操作结束         RowCount = (int)cmd.Parameters["@rowcount"].Value;
        PageCount =(int)cmd.Parameters["@pagecount"].Value;
        
        if (CurrentPage>PageCount)
        {
            Response.Redirect("CutPage.aspx?CurrentPage="+Convert.ToString(PageCount));
            Response.End();
        }
        this.lblCurrent.Text = Convert.ToString(CurrentPage);
        this.lblPageTotal.Text = Convert.ToString(PageCount);
        this.lblRowsTotal.Text = Convert.ToString(RowCount);
        this.lblPageSize.Text = Convert.ToString(PageSize);         this.hlFirst.NavigateUrl = "CutPage.aspx?CurrentPage=1";
        this.hlPrev.NavigateUrl = "CutPage.aspx?CurrentPage=" + Convert.ToString(CurrentPage-);
        this.hlNext.NavigateUrl = "CutPage.aspx?CurrentPage=" + Convert.ToString(CurrentPage+);
        this.hlLast.NavigateUrl = "CutPage.aspx?CurrentPage=" + Convert.ToString(PageCount);
        if (Convert.ToInt32(CurrentPage) == )
        {
            this.hlPrev.Enabled = false;
            this.hlFirst.Enabled = false;
        }
        if (Convert.ToInt32(CurrentPage) == PageCount)
        {
            this.hlNext.Enabled = false;
            this.hlLast.Enabled = false;
        }         GridView1.DataSource = ds.Tables[];
        GridView1.DataBind();
    }