一个最简单的存储过程分页

 学习存储过程,先做一个最简单的分页存储过过程吧!

create proc usp_fenyue  --定义存储过程名
  @pageindex int,     --定义参数当前页
  @pagesize int,      --定义参数每页多少行
  @pagecount int output   --定义一个输出参数,总页数
  as
  declare @count int   --定义一个参数,查询总共多少行

   --分页查询
  select * from (select *,ROW_NUMBER() over(order by id)as number from tab2)as A
  where number between (@pagesize*(@pageindex-1)+1) and @pageindex*@pagesize
 

--查询总页数
    select @count=COUNT(*) from tab2  
    set @pagecount=ceiling(@count*1.0/@pagesize)

    select @pagecount

--在sqlserver 执行存储过程

  declare @dd int
 
 exec usp_fenyue 1,3, @dd output      -- 有几个参数就写几个参数,注意写输出参数的时候,要定义变量,要写上output!

现在一个最简单的分页存储过程就写完了,然后我们在页面调用!

  还是先建一个最简单的aspx页面,如下,

 1 <div>
 2         <asp:GridView ID="GridView1" runat="server">
 3         </asp:GridView><br/>
 4         <asp:Button ID="Butshang" runat="server" Text="上一页" onclick="Butshang_Click" />
 5         <asp:Button ID="Butxia"
 6             runat="server" Text="下一页" onclick="Butxia_Click" /> 
 7         <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
 8     </div>
 9     <asp:HiddenField ID="HiddenField1" runat="server" />
10     <asp:HiddenField ID="HiddenField2" runat="server" />

然后写后台代码,这里没有使用3层来做,用的最基础的方法做的分页,这样可更好的去学习ADO.NET的底层

 1  public partial class WebForm3 : System.Web.UI.Page
 2     {
 3         int pageindex = 1; //起始页
 4         int pagesize = 3;  //每页多少条
 5         int pagecount = 0; //一共多少页
 6         string connstr = @"Data Source=JYZ\SQL2008;Initial Catalog=123;User ID=sa;Password=123456"; //数据库连接字符串
 7         protected void Page_Load(object sender, EventArgs e)
 8         {
 9             if (!IsPostBack) 
10             {
11                 BidGiv();
12             }
13         }
14 
15         /// <summary>
16         /// 绑定GridView
17         /// </summary>
18         private void BidGiv()
19         {
20             using (SqlConnection conn = new SqlConnection(connstr)) //打开连接
21             {
22                 using (SqlCommand cmd = new SqlCommand("usp_fenyue", conn)) //执行方法
23                 {
24                     cmd.CommandType = CommandType.StoredProcedure; //执行存储过程usp_fenyue
25 
26                     //创建存储过程所需要的参数
27                     SqlParameter[] param ={ 
28                                     new SqlParameter("@pageindex",SqlDbType .Int),
29                                     new SqlParameter ("@pagesize",SqlDbType .Int ),
30                                     new SqlParameter ("@pagecount",SqlDbType .Int )
31                     
32                     }; 
33 
34                     param[0].Value = pageindex;
35                     param[1].Value = pagesize;
36                     param[2].Direction = ParameterDirection.Output; //输出参数
37 
38 
39                     cmd.Parameters.AddRange(param); //把参数加到执行方法中
40 
41                     SqlDataAdapter sb = new SqlDataAdapter(cmd); //创建一个数据集
42 
43                     DataTable tb = new DataTable(); //创建一个TataTable存放数据
44                    
45 
46                     sb.Fill(tb);  //把查询出来的数据放到tb中
47 
48                     GridView1.DataSource = tb; //把tb绑定到GridView1中
49                     GridView1.DataBind();
50 
51                     pagecount = Convert.ToInt32(param[2].Value); //得到总页数
52 
53                     this.HiddenField1.Value = pagecount.ToString(); //保存总页数在分页时用
54                     this.HiddenField2.Value = pageindex.ToString(); //保存当前页在分页时用
55 
56                     this.Label1.Text = "当前页" + pageindex +"/"+ "总页数" + pagecount; 
57                 }
58             }
59         
60         }
61         /// <summary>
62         /// 上一页
63         /// </summary>
64         /// <param name="sender"></param>
65         /// <param name="e"></param>
66         protected void Butshang_Click(object sender, EventArgs e)
67         {
68             if (Convert.ToInt32(this.HiddenField2.Value) > 1)//如果当前页大于1,就把当前页减1,再绑定
69             {
70                 pageindex = Convert.ToInt32(this.HiddenField2.Value) - 1;
71                 BidGiv();
72             }
73 
74         }
75         /// <summary>
76         /// 下一页
77         /// </summary>
78         /// <param name="sender"></param>
79         /// <param name="e"></param>
80         protected void Butxia_Click(object sender, EventArgs e)
81         {
82             if (Convert .ToInt32(this.HiddenField2.Value) < Convert.ToInt32(this.HiddenField1.Value))
83             {
84                 pageindex = Convert.ToInt32(this.HiddenField2.Value) + 1;//如果当前页小于总页,就把当前页加1,再绑定
85                 BidGiv();
86             }
87         }
88     }

运行页面,一个用存储过程做的分页,就做好了!


 

原文地址:https://www.cnblogs.com/xu3593/p/2830727.html