利用网上的一个存储过程(该存储过程需要用到有唯一的整型主键的表,然后按该主键进行排序,这是一个不足):
网页调用:
1、返回记录数
2、返回记录集
1ALTER PROCEDURE pagination
2@tblName varchar(255), -- 表名
3@strGetFields varchar(1000) = '*', -- 需要返回的列
4@fldName varchar(255)='', -- 排序的字段名
5@PageSize int , -- 页尺寸
6@PageIndex int, -- 页码
7@doCount bit , -- 返回记录总数, 非 0 值则返回
8@OrderType bit , -- 设置排序类型, 非 0 值则降序
9@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
10AS
11declare @strSQL varchar(5000) -- 主语句
12declare @strTmp varchar(110) -- 临时变量
13declare @strOrder varchar(400) -- 排序类型
14
15if @doCount != 0
16 begin
17 if @strWhere !=''
18 set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere
19 else
20 set @strSQL = "select count(*) as Total from [" + @tblName + "]"
21 end
22 --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
23else
24 begin
25
26 if @OrderType != 0
27 begin
28 set @strTmp = "<(select min"
29 set @strOrder = " order by [" + @fldName +"] desc"
30 --如果@OrderType不是0,就执行降序,这句很重要!
31 end
32 else
33 begin
34 set @strTmp = ">(select max"
35 set @strOrder = " order by [" + @fldName +"] asc"
36 end
37
38 if @PageIndex = 1
39 begin
40 if @strWhere != ''
41 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where " + @strWhere + " " + @strOrder
42 else
43 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["+ @tblName + "] "+ @strOrder
44 --如果是第一页就执行以上代码,这样会加快执行速度
45 end
46 else
47 begin
48 --以下代码赋予了@strSQL以真正执行的SQL代码
49 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["
50 + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder
51
52 if @strWhere != ''
53 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["
54 + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
55 + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
56 + @fldName + "] from [" + @tblName + "] where " + @strWhere + " "
57 + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder
58 end
59 end
60exec (@strSQL)
2@tblName varchar(255), -- 表名
3@strGetFields varchar(1000) = '*', -- 需要返回的列
4@fldName varchar(255)='', -- 排序的字段名
5@PageSize int , -- 页尺寸
6@PageIndex int, -- 页码
7@doCount bit , -- 返回记录总数, 非 0 值则返回
8@OrderType bit , -- 设置排序类型, 非 0 值则降序
9@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)
10AS
11declare @strSQL varchar(5000) -- 主语句
12declare @strTmp varchar(110) -- 临时变量
13declare @strOrder varchar(400) -- 排序类型
14
15if @doCount != 0
16 begin
17 if @strWhere !=''
18 set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere
19 else
20 set @strSQL = "select count(*) as Total from [" + @tblName + "]"
21 end
22 --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
23else
24 begin
25
26 if @OrderType != 0
27 begin
28 set @strTmp = "<(select min"
29 set @strOrder = " order by [" + @fldName +"] desc"
30 --如果@OrderType不是0,就执行降序,这句很重要!
31 end
32 else
33 begin
34 set @strTmp = ">(select max"
35 set @strOrder = " order by [" + @fldName +"] asc"
36 end
37
38 if @PageIndex = 1
39 begin
40 if @strWhere != ''
41 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where " + @strWhere + " " + @strOrder
42 else
43 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["+ @tblName + "] "+ @strOrder
44 --如果是第一页就执行以上代码,这样会加快执行速度
45 end
46 else
47 begin
48 --以下代码赋予了@strSQL以真正执行的SQL代码
49 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["
50 + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder
51
52 if @strWhere != ''
53 set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["
54 + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
55 + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
56 + @fldName + "] from [" + @tblName + "] where " + @strWhere + " "
57 + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder
58 end
59 end
60exec (@strSQL)
网页调用:
1 private static DataSet GetCustomersData(string tblName, string strGetFields, string fldName, int PageSize, int PageIndex, int doCount, int OrderType, string strWhere)
2 {
3 string connString = ConfigurationSettings.AppSettings["connstr"];
4 SqlConnection conn = new SqlConnection(connString);
5 SqlCommand comm = new SqlCommand("pagination", conn);
6
7 comm.Parameters.Add(new SqlParameter("@tblName", SqlDbType.VarChar));//表名
8 comm.Parameters[0].Value = tblName;
9 comm.Parameters.Add(new SqlParameter("@strGetFields", SqlDbType.VarChar));//返回的列
10 comm.Parameters[1].Value = strGetFields;
11 comm.Parameters.Add(new SqlParameter("@fldName", SqlDbType.VarChar));//排序的字段名
12 comm.Parameters[2].Value = fldName;
13 comm.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int));//页尺寸
14 comm.Parameters[3].Value = PageSize;
15 comm.Parameters.Add(new SqlParameter("@PageIndex", SqlDbType.Int));//页码
16 comm.Parameters[4].Value = PageIndex;
17 comm.Parameters.Add(new SqlParameter("@doCount", SqlDbType.Int));//是否返回记录总数,0为不返回,1为返回
18 comm.Parameters[5].Value = doCount;
19 comm.Parameters.Add(new SqlParameter("@OrderType", SqlDbType.Int));//设置排序类型,0为升序,非0为降序
20 comm.Parameters[6].Value = OrderType;
21 comm.Parameters.Add(new SqlParameter("@strWhere", SqlDbType.VarChar));//where语句
22 comm.Parameters[7].Value = strWhere;
23
24 comm.CommandType = CommandType.StoredProcedure;
25
26 SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
27 DataSet ds = new DataSet();
28 dataAdapter.Fill(ds);
29
30 return ds;
31 }
具体调用:2 {
3 string connString = ConfigurationSettings.AppSettings["connstr"];
4 SqlConnection conn = new SqlConnection(connString);
5 SqlCommand comm = new SqlCommand("pagination", conn);
6
7 comm.Parameters.Add(new SqlParameter("@tblName", SqlDbType.VarChar));//表名
8 comm.Parameters[0].Value = tblName;
9 comm.Parameters.Add(new SqlParameter("@strGetFields", SqlDbType.VarChar));//返回的列
10 comm.Parameters[1].Value = strGetFields;
11 comm.Parameters.Add(new SqlParameter("@fldName", SqlDbType.VarChar));//排序的字段名
12 comm.Parameters[2].Value = fldName;
13 comm.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int));//页尺寸
14 comm.Parameters[3].Value = PageSize;
15 comm.Parameters.Add(new SqlParameter("@PageIndex", SqlDbType.Int));//页码
16 comm.Parameters[4].Value = PageIndex;
17 comm.Parameters.Add(new SqlParameter("@doCount", SqlDbType.Int));//是否返回记录总数,0为不返回,1为返回
18 comm.Parameters[5].Value = doCount;
19 comm.Parameters.Add(new SqlParameter("@OrderType", SqlDbType.Int));//设置排序类型,0为升序,非0为降序
20 comm.Parameters[6].Value = OrderType;
21 comm.Parameters.Add(new SqlParameter("@strWhere", SqlDbType.VarChar));//where语句
22 comm.Parameters[7].Value = strWhere;
23
24 comm.CommandType = CommandType.StoredProcedure;
25
26 SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
27 DataSet ds = new DataSet();
28 dataAdapter.Fill(ds);
29
30 return ds;
31 }
1、返回记录数
int pageSize=10;
int pageIndex=1;
int recordCount = Convert.ToInt32(GetCustomersData("Categories", "*", "CategoryID", pageSize, pageIndex, 1, 0, "").Tables[0].Rows[0][0].ToString());
int pageIndex=1;
int recordCount = Convert.ToInt32(GetCustomersData("Categories", "*", "CategoryID", pageSize, pageIndex, 1, 0, "").Tables[0].Rows[0][0].ToString());
2、返回记录集
int pageSize=10;
int pageIndex=1;
DataSet ds = GetCustomersData("Categories", "*", "CategoryID", pageSize, pageIndex, 1, 0, "");
int pageIndex=1;
DataSet ds = GetCustomersData("Categories", "*", "CategoryID", pageSize, pageIndex, 1, 0, "");