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

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];
                }
                
            }
        }

运行后:



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


create table MyStudent(sId int identity primary key,sName nvarchar(50),sAge int,sSex nchar(2),sEnglish float,sMath float)
select * from MyStudent

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

insert into MyStudent(sName,sAge,sSex,sEnglish,sMath)values('范苑莲',41,0,34,18)
insert into MyStudent(sName,sAge,sSex,sEnglish,sMath)values('霍颖瑞',64,0,53,53)
insert into MyStudent(sName,sAge,sSex,sEnglish,sMath)values('饶荣娣',107,0,88,72)
insert into MyStudent(sName,sAge,sSex,sEnglish,sMath)values('陆蓓春',87,0,72,9)
insert into MyStudent(sName,sAge,sSex,sEnglish,sMath)values('

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值