您的位置:首页 > 数据库

c# + sql procedure 实现分页

2014-01-16 14:07 211 查看
在开始使用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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#+procedure 分页