c# + sql procedure 实现分页

本文介绍如何在 SQL Server 中通过启用 CLR 集成特性来使用 C# 编写的存储过程实现分页功能,并提供了创建存储过程及在 C# 中调用的具体步骤。

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

在开始使用c#写存储过程之前,必须要启用SQL Server的CLR集成特性。默认情况下它是不启用的。可以通过以下指令:

sp_configure ‘clr enabled’ ,1

GO

RECONFIGURE

GO

这里,我们执行了系统存储过程(需要在master之下)"sp_configure",为其提供两个参数分别是“clr_enabled” 和“1”。如果要停用CLR集成的话也是整形这个存储过程,不过第二个参数要变成“0”。为了使新的设置产生效果,不要忘记调用RECONFIGURE。

1.新建存储过程 pro_one,作用是分页

create procedure pro_one
@pageindex int =1,//第几页
@pagenum int =5//每页元素数
as 
select top(@pagenum) * from
(
select Row_number() over (order by id desc) as r,* from sps_login

/*over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用。
其参数:over(partition by columnname1 order by columnname2)
含义:按columname1指定的字段进行分组排序,或者说按字段columnname1的值进行分组排序。*/
) as p
where p.r>(@pageindex-1)*@pagenum and p.r<=@pageindex*@pagenum

2.在c#中调用

           if (conn.State!=ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand comm = new SqlCommand("pro_one", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.UpdatedRowSource = UpdateRowSource.None;
            comm.Parameters.Add(new SqlParameter("@pageindex", SqlDbType.Int));
            comm.Parameters.Add(new SqlParameter("@pagenum", SqlDbType.Int));
            comm.Parameters["@pageindex"].Value = textBox1.Text;//页数
            comm.Parameters["@pagenum"].Value = textBox2.Text; //元素数
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = comm;
            sda.SelectCommand.ExecuteNonQuery();
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值