日期:2014-05-17  浏览次数:20574 次

sql 分页查询
SQL code



USE [guestbook]
GO
/****** Object:  StoredProcedure [dbo].[selectmessage]    Script Date: 08/27/2012 21:52:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[selectmessage]
 @pageSize int,
 @pageIndex int
as
declare @sql nvarchar(200)
if(@pageIndex<2)
begin
set @sql='select top '+convert(varchar(10),@pageSize)+' * from gbook'
end
else
begin
set @sql='select top '+convert(varchar(10),@pageSize)+' * from gbook where
id>(select max(id) from (
  select top '+convert(varchar(10),@pageSize*(@pageIndex-1))+' id from gbook order by id) a)order by id'
end
exec sp_executesql @sql





C# code

 //进行数据绑定的方法
        public void Bind(int x,int y)
        {

            //objPds.DataSource = this.SqlDataSource1.Select(new DataSourceSelectArguments());
            int CurrentPage = Convert.ToInt32(labNowPage.Text);
            PagedDataSource ps = new PagedDataSource();//生成PagedDataSource的实例
            //实例化SqlConnection对象
            SqlConnection sqlCon = new SqlConnection();
            
            sqlCon.ConnectionString = "server=.;uid=sa;pwd=sa;database=guestbook";
            //定义SQL语句
            //string SqlStr = "select * from gbook order by id desc";
            string SqlStr = "exec selectmessage @pageSize="+x+",@pageIndex="+y+"";
            //实例化SqlDataAdapter对象
            SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
            //实例化数据集DataSet
            DataSet ds = new DataSet();
            da.Fill(ds, "gbook");
            ps.DataSource = ds.Tables["gbook"].DefaultView;
            //ps.AllowPaging = true; //是否可以分页
            //ps.PageSize = 2; //显示的数量
            ps.CurrentPageIndex = CurrentPage - 1; //取得当前页的页码
            lnkbtnFront.Enabled = true;
            lnkbtnFirst.Enabled = true;
            lnkbtnNext.Enabled = true;
            lnkbtnLast.Enabled = true;
            
            if (CurrentPage == 1)
            {
                lnkbtnFirst.Enabled = false;//不显示第一页按钮
                lnkbtnFront.Enabled = false;//不显示上一页按钮
            }
            if (CurrentPage == ps.PageCount)
            {
                lnkbtnNext.Enabled = false;//不显示下一页
                lnkbtnLast.Enabled = false;//不显示最后一页

            }
            this.labCount.Text = Convert.ToString(ps.PageCount);
            this.DataList1.DataSource = ps;
            this.DataList1.DataKeyField = "id";
            this.DataList1.DataBind();

            if (Session["username"] == null)
            {
                //Panel1.Visible = false;


            }
            else
            {
                Label1.Visible = true;
                Label1.Text = "欢迎您!" + Session["username"].ToString();
                Button1.Visible = true;

                Label6.Visible = true;
                Label7.Visible = true;
                TextBox2.Visible = true;
                //TextBox3.Visible = true;
                FCKeditor1.Visible = true;
                Button6.Visible = true;
                // Button9.Visible = true;
                Button4.Visible = true;
            }

            if (Session["admin"] == null)
            {
                // DataList1.FindControl("Button2").Visible = false;
                //  DataList1.FindControl("Button3").Visible = false;
                //  DataList1.FindControl("TextBox1").Visible = false;
            }
            else
            {
                Label1.Visible = true;
                Label1.Text = "欢迎您!" + Session["admin"].ToString();
                Button1.Visible = true;