对特定表进行分页显示的存储过程(要求有两个参数一个是 每页显示的记录的条数(@pagesize),第二个是显示第几页(@pageIndex))

本文展示了如何在SQL Server中创建一个名为usp_MyStudent_GetDateByPageIndex的存储过程,该过程用于实现对MyStudent表的分页显示。它接受两个参数:每页显示的记录数和当前页码,然后利用ROW_NUMBER()函数和WHERE子句来获取指定范围的数据。在.NET应用程序中,使用C#和ADO.NET的SqlCommand对象调用此存储过程,并填充到DataSet中展示数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sql Server 存储过程:

create proc usp_MyStudent_GetDateByPageIndex

@pageSize int,
@pageIndex int
as
begin
  select * from
  (select *,ROW_NUMBER() over(order by sId) as rowIndex
   from MyStudent) as tb1
  where tb1.rowIndex between (@pageSize*(@pageIndex-1))+1 and @pageSize*@pageIndex
end

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


app.configer:

<connectionStrings>
 <add name="MySchoolconStr" connectionString="Data Source=PC_THINK-THINK;Initial Catalog=MySchool;User ID=sa; Password=111111"/>
  </connectionStrings>

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

后台代码:

       string Constr = ConfigurationManager.ConnectionStrings["MySchoolconStr"].ConnectionString;

        DataSet ds = new DataSet();
        private void btnShow_Click(object sender, EventArgs e)
        {
           // DataTable table = new DataTable();
            ds.Clear();
            //dataGridView1.DataSource = table;
            using (SqlConnection conn = new SqlConnection(Constr))
            {
                conn.Open();
                string sp_name = "usp_MyStudent_GetDateByPageIndex";
                using (SqlCommand cmd = new SqlCommand(sp_name, conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = sp_name;
                    cmd.Parameters.Add(new SqlParameter("@pageSize", txtpageSize.Text));
                    cmd.Parameters.Add(new SqlParameter("@pageIndex", txtpageIndex.Text));
                    SqlDataAdapter data = new SqlDataAdapter(cmd);
                    data.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
                
            }
        }

运行后:



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值