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
) 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];
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#实现Datagridview分页,从Sql中读取的数据
- c#学习之-c#通过sql存储过程实现分页
- C#通过SQL语句查询结果分页不用GridView的实现
- 通用分页SQL(不要存储过程)(C#实现)(
- C#拼接SQL语句,SQL Server 2005+,多行多列大数据量情况下,使用ROW_NUMBER实现的高效分页排序
- 利用SQL未公开的存储过程实现分页
- c#实现google样式的分页
- sql语句实现分页技术
- Oracle SQL实现分页查询
- c# 程序实现ACCESS数据导入SQL的方法
- SQL语句实现数据分页
- J2EE (五) SQL+PageClass实现真分页
- c# sql数据分页的应用
- 内容分页简单实现代码及祥解(C#)
- 内容分页简单实现代码及祥解(C#)
- c# 执行多条SQL语句,实现数据库事务 线性表 Hashtable
- 查询分页的SQL实现
- Oracle中实现分页的SQL语句
- Hibernate使用原生的动态sql实现带条件的查询分页功能