标签:
我在这使用SQL server数据库和我已有的数据库中的表。表名: HKSJ_Main
1、创建存储过程
--该分页的原理 越过多少条,去多少条
create proc P_page
--声明参数
@size int , --一页取几条
@pageindex int,--取第几页
@total int out --返回总条数
as
begin
select top(@size)* from dbo.HKSJ_Main as B where B.ID not in
(
select top((@pageindex-1)*@size) a.ID from dbo.HKSJ_Main as A order by A.ID
)
order by B.ID
select @total = COUNT(*) from dbo.HKSJ_Main
end
2、测试存储过程是否可用
--测试执行
declare @total int
exec P_page 5,2,@total out
select * from dbo.HKSJ_Main
3、封装到C#代码中
1 public DataSet Getmypage(int pagesize, int pageindex, out int pageCounte) 2 { 3 pageCounte = 0; 4 //DataSet ds = DbHelperSQL.ExecProc("P_page"); 5 DataSet ds = new DataSet(); 6 using (SqlDataAdapter sa = new SqlDataAdapter("P_page", new SqlConnection(DbHelperSQL.connectionString))) 7 { 8 sa.SelectCommand.CommandType = CommandType.StoredProcedure; 9 sa.SelectCommand.Parameters.Add(new SqlParameter("@size",pagesize)); 10 sa.SelectCommand.Parameters.Add(new SqlParameter("@pageindex", pageindex)); 11 SqlParameter outparam = new SqlParameter("@total", SqlDbType.Int); 12 outparam.Direction = ParameterDirection.Output; 13 sa.SelectCommand.Parameters.Add(outparam); 14 sa.Fill(ds); 15 pageCounte = (int)outparam.Value; 16 } 17 System.Collections.Generic.List<Model.HKSJ_Main> list = new System.Collections.Generic.List<Model.HKSJ_Main>(); 18 19 return ds; 20 }
在网页后台代码调用该方法
public List<Model.HKSJ_Main> detailsNewsList { get; set; } protected int pageCounte; protected int pageindex; protected int pagesize; protected void Page_Load(object sender, EventArgs e) { pagesize = 5; pageindex = int.Parse(Request["pageindex"] ?? "1"); //一下就是调用该方法,在调用前已经将其中的对象取出,放入到了对应的Modle中,detailsNewsList Maticsoft.BLL.HKSJ_Main newsList = new BLL.HKSJ_Main(); detailsNewsList = newsList.Getmypage(pagesize, pageindex, out pageCounte); //获取页数 pageCounte = ((pageCounte + pagesize) - 1) / pagesize; if (pageindex < 0 || pageindex > pageCounte) { pageindex = 1; detailsNewsList = newsList.Getmypage(pagesize, pageindex, out pageCounte); pageCounte = ((pageCounte + pagesize) - 1) / pagesize; }
打开网页测试
成功。
标签:
原文地址:http://www.cnblogs.com/LijiafengBlog/p/5300163.html