1.得先建立数据库分页存储过程,具体如下:
1 -- Author: zxj
2 -- Create date: 2012-03-23
3 -- Description: 分页,用到了ROW_NUMBER()
4 -- =============================================
5 ALTER PROCEDURE [dbo].[proc_SplitPage]
6 @tblName varchar(255), -- 表名
7 @strFields varchar(1000) = '*', -- 需要返回的列,默认*
8 @strOrder varchar(255)='', -- 排序的字段名,必填
9 @strOrderType varchar(10)='ASC', -- 排序的方式,默认ASC
10 @PageSize int = 10, -- 页尺寸,默认10
11 @PageIndex int = 1, -- 页码,默认1
12 @strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
13 AS
14
15 declare @strSQL varchar(5000)
16
17 if @strWhere !=''
18 set @strWhere=' where '+@strWhere
19
20 set @strSQL=
21 'SELECT c.name,* FROM ('+
22 'SELECT ROW_NUMBER() OVER (ORDER BY '+@strOrder+' '+@strOrderType+') AS pos,'+@strFields+' '+
23 'FROM '+@tblName+' '+@strWhere+
24 ') AS sp
25 left join cateGory c on c.id=sp.caid
26 WHERE pos BETWEEN '+str((@PageIndex-1)*@PageSize+1)+' AND '+str(@PageIndex*@PageSize)
27
28 exec (@strSQL)
2.为了思路的清晰,使用了三层结构,从上往下开始介绍。
1 表示层(WEB)代码片段
2 private string getWhere()//查询条件
3 {
4 string strwhere = "";//默认情况
5 if (txtKey.Text.Trim().Length != 0)判断文本框中是否有值
6 {
7 string key = txtKey.Text.Trim();//通过得到文本框的值得到查询条件
8 where = "linkname like '%" + key + "%' or url like '%" + key + "%'";//拼接查询条件的SQL语句,小心SQL注入
9 }
10 return strwhere;
11 }
12
13 private void bindData()
14 {
15 AspNetPager1.RecordCount = new NewsManager().RecordCount(getWhere());//通过条件来的到总的记录数
16 //绑定数据
17 gvList.DataSource = new NewsManager().SelectNewsBySplitPage(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, getWhere());
18 gvList.DataBind();
19 }
1 业务逻辑层层(BLL)的代码片段
2 /// <summary>
3 /// 返回总记录数
4 /// </summary>
5 /// <returns></returns>
6 public int RecordCount(string strwhere)
7 {
8 return NewsDAO.SelectCount(strwhere);
9 }
10 /// <summary>
11 /// 返回分页
12 /// </summary>
13 /// <returns></returns>
14 public DataTable SelectNewsBySplitPage(int pageSize, int pageIndex,string strwhere)
15 {
16 return NewsDAO.SelectNewsBySplitPage(pageSize, pageIndex, strwhere);
17 }
3.下面是 数据访问层层(DAL)的代码片段,分为DBHelper类和DAO实用类。
1 实用类(DAO):
2 /// <summary>
3 /// 查询新闻的总数
4 /// </summary>
5 /// <param name="strWhere"></param>
6 /// <returns></returns>
7 public static int SelectCount(string strwhere)
8 {
9 string sql;
10 if (!string.IsNullOrEmpty(strwhere))
11 {
12 sql = "select count(id) from news where " + strwhere;
13 }
14 else
15 { sql = "select count(id) from news"; }
16 return (int)DBHelper.ExecuteScalar(sql, CommandType.Text);
17 }
18
19 /// <summary>
20 /// 分页
21 /// </summary>
22 /// <param name="pageSize">分页大小</param>
23 /// <param name="pageIndex">分页索引</param>
24 /// <param name="strWhere">分页条件</param>
25 /// <returns></returns>
26 public static DataTable SelectNewsBySplitPage(int pageSize, int pageIndex, string strwhere)
27 {
28 SqlParameter[] paras = new SqlParameter[]{
29 new SqlParameter("@tblName","news"),
30 new SqlParameter("@strFields","*"),
31 new SqlParameter("@strOrder","id"),
32 new SqlParameter("@strOrderType","desc"),
33
34 new SqlParameter("@PageSize",pageSize),
35 new SqlParameter("@PageIndex", pageIndex),
36 new SqlParameter("@strWhere", strWhere)
37 };
38 return DBHelper.ExecuteDataTable("proc_SplitPage", CommandType.StoredProcedure,paras);
39 }
1 通用类(DBHelper)
2 public static int ExecuteScalar(string sql, CommandType ct,params SqlParameter[] values)
3 {
4 SqlConnection conn = StrConn();
5 conn.Open();
6 using (SqlCommand cmd = new SqlCommand(sql, conn))
7 {
8 cmd.Parameters.AddRange(values);
9 cmd.CommandType = ct;
10 return (int)cmd.ExecuteScalar();
11 }
12 }
13
14 /// <summary>
15 /// 返回DataTable
16 /// </summary>
17 /// <param name="sql"></param>
18 /// <param name="ct"></param>
19 /// <param name="values"></param>
20 /// <returns></returns>
21 public static DataTable ExecuteDataTable(string sql, CommandType ct,params SqlParameter[] values)
22 {
23 SqlConnection conn = StrConn();
24 conn.Open();
25 DataTable dt = new DataTable();
26 using (SqlCommand cmd = new SqlCommand(sql, conn))
27 {
28 cmd.Parameters.AddRange(values);
29 cmd.CommandType = ct;
30 SqlDataReader reader = cmd.ExecuteReader();
31 dt.Load(reader);
32 return dt;
33 }
34 }
最后呢,最好从下往上再走一遍,切不可粘贴复制到自己的代码中,也许我写的难免会有一些错误!呵呵,第一篇算是自己写的了!