成就别人、等于成就自己。我没什么要求,欢迎大家多多支持与评论,觉得不错的,记得点击文章左下角的”关注博客”,就这么简单。同时,你要用什么好的想法,也可以与我交流,谢谢。
分页控件运行效果如下图:
用到的分页存储过程:
1-- =============================================
2-- Author: EricHu
3-- Create date: 2008-10-25
4-- Description: 千万数量级分页存储过程
5-- Modify Date: 2010-10-26
6-- =============================================
7
8 SET ANSI_NULLS ON
9 GO
10 SET QUOTED_IDENTIFIER ON
11 GO
12
13 ALTERPROCEDURE[dbo].[uspDividePage]
14/*
15 ***************************************************************
16 ** 千万数量级分页存储过程**
17 ***************************************************************
18 参数说明:
19 1.Tables :表名或视图名
20 2.PrimaryKey :主关键字
21 3.Sort :排序语句,不带Order By 比如:UserId Desc,CreateDate Asc
22 4.CurrentPage :当前页码
23 5.PageSize :分页尺寸
24 6.Fields :字段列表(默认为:*)
25 7.Filter :过滤语句,不带Where
26 8.Group :Group语句,不带Group By
27 ***************************************************************/
28 (
29@Tablesvarchar(2000),
30@PrimaryKeyvarchar(500),
31@Sortvarchar(500) =NULL,
32@CurrentPageint=1,
33@PageSizeint=10,
34@Fieldsvarchar(2000) ='*',
35@Filtervarchar(1000) =NULL,
36@Groupvarchar(1000) =NULL
37 )
38AS
39/*默认排序*/
40IF@SortISNULLOR@Sort=''
41SET@Sort=@PrimaryKey
42
43DECLARE@SortTablevarchar(1000)
44DECLARE@SortNamevarchar(1000)
45DECLARE@strSortColumnvarchar(1000)
46DECLARE@operatorchar(2)
47DECLARE@typevarchar(1000)
48DECLARE@precint
49
50/*设定排序语句.*/
51IFCHARINDEX('DESC',@Sort)>0
52BEGIN
53SET@strSortColumn=REPLACE(@Sort, 'DESC', '')
54SET@operator='<='
55END
56ELSE
57BEGIN
58IFCHARINDEX('ASC', @Sort) =0
59SET@strSortColumn=REPLACE(@Sort, 'ASC', '')
60SET@operator='>='
61END
62
63IFCHARINDEX('.', @strSortColumn) >0
64BEGIN
65SET@SortTable=SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
66SET@SortName=SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) +1, LEN(@strSortColumn))
67END
68ELSE
69BEGIN
70SET@SortTable=@Tables
71SET@SortName=@strSortColumn
72END
73
74SELECT@type=t.name, @prec=c.prec
75FROM sysobjects o
76JOIN syscolumns c on o.id=c.id
77JOIN systypes t on c.xusertype=t.xusertype
78WHERE o.name =@SortTableAND c.name =@SortName
79
80IFCHARINDEX('char', @type) >0
81SET@type=@type+'('+CAST(@precASvarchar) +')'
82
83DECLARE@strPageSizevarchar(500)
84DECLARE@strStartRowvarchar(500)
85DECLARE@strFiltervarchar(1000)
86DECLARE@strSimpleFiltervarchar(1000)
87DECLARE@strGroupvarchar(1000)
88/*默认当前页*/
89IF@CurrentPage<1
90SET@CurrentPage=1
91
92/*设置分页参数.*/
93SET@strPageSize=CAST(@PageSizeASvarchar(500))
94SET@strStartRow=CAST(((@CurrentPage-1)*@PageSize+1) ASvarchar(500))
95
96/*筛选以及分组语句.*/
97IF@FilterISNOTNULLAND@Filter!=''
98BEGIN
99SET@strFilter=' WHERE '+@Filter+''
100SET@strSimpleFilter=' AND '+@Filter+''
101END
102ELSE
103BEGIN
104SET@strSimpleFilter=''
105SET@strFilter=''
106END
107
108IF@GroupISNOTNULLAND@Group!=''
109SET@strGroup=' GROUP BY '+@Group+''
110ELSE
111SET@strGroup=''
112
113/*执行查询语句*/
114EXEC(
115'
116 DECLARE @SortColumn '+@type+'
117 SET ROWCOUNT '+@strStartRow+'
118 SELECT @SortColumn='+@strSortColumn+' FROM '+@Tables+@strFilter+''+@strGroup+' ORDER BY '+@Sort+'
119 SET ROWCOUNT '+@strPageSize+'
120 SELECT '+@Fields+' FROM '+@Tables+' WHERE '+@strSortColumn+@operator+' @SortColumn '+@strSimpleFilter+''+@strGroup+' ORDER BY '+@Sort+'
121')
122GO
123
124SET ANSI_NULLS OFF
125GO
126SET QUOTED_IDENTIFIER OFF
127GO
128
本控件类图
本控件UcpageControl类详细信息
本控件PageData类详细信息
本控件核心代码
一、数据源提供类PageData,主要负责与存储过程进行交互。
1#region 数据源提供(PageData)
2///<summary>
3/// 数据源提供
4///</summary>
5publicclass PageData
6 {
7 DataSet ds =null;
8privateint _PageSize =50; //分页大小
9privateint _PageIndex =1; //当前页
10privateint _PageCount =0; //总页数
11privateint _TotalCount =0; //总记录数
12privatestring _QueryFieldName ="*"; //表字段FieldStr
13privatebool _isQueryTotalCounts =true; //是否查询总的记录条数
14privatestring _TableName =string.Empty; //表名
15privatestring _OrderStr =string.Empty; //排序_SortStr
16privatestring _QueryCondition =string.Empty; //查询的条件 RowFilter
17privatestring _PrimaryKey =string.Empty; //主键
18
19///<summary>
20/// 是否查询总的记录条数
21///</summary>
22publicbool IsQueryTotalCounts
23 {
24get { return _isQueryTotalCounts; }
25set { _isQueryTotalCounts = value; }
26 }
27
28///<summary>
29/// 分页大小(每页显示多少条数据)
30///</summary>
31publicint PageSize
32 {
33get
34 {
35return _PageSize;
36
37 }
38set
39 {
40 _PageSize = value;
41 }
42 }
43
44///<summary>
45/// 当前页
46///</summary>
47publicint PageIndex
48 {
49get
50 {
51return _PageIndex;
52 }
53set
54 {
55 _PageIndex = value;
56 }
57 }
58
59///<summary>
60/// 总页数
61///</summary>
62publicint PageCount
63 {
64get
65 {
66return _PageCount;
67 }
68 }
69
70///<summary>
71/// 总记录数
72///</summary>
73publicint TotalCount
74 {
75get
76 {
77return _TotalCount;
78 }
79 }
80
81///<summary>
82/// 表名或视图名
83///</summary>
84publicstring TableName
85 {
86get
87 {
88return _TableName;
89 }
90set
91 {
92 _TableName = value;
93 }
94 }
95
96///<summary>
97/// 表字段FieldStr
98///</summary>
99publicstring QueryFieldName
100 {
101get
102 {
103return _QueryFieldName;
104 }
105set
106 {
107 _QueryFieldName = value;
108 }
109 }
110
111///<summary>
112/// 排序字段
113///</summary>
114publicstring OrderStr
115 {
116get
117 {
118return _OrderStr;
119 }
120set
121 {
122 _OrderStr = value;
123 }
124 }
125
126///<summary>
127/// 查询条件
128///</summary>
129publicstring QueryCondition
130 {
131get
132 {
133return _QueryCondition;
134 }
135set
136 {
137 _QueryCondition = value;
138 }
139 }
140
141///<summary>
142/// 主键
143///</summary>
144publicstring PrimaryKey
145 {
146get
147 {
148return _PrimaryKey;
149 }
150set
151 {
152 _PrimaryKey = value;
153 }
154 }
155
156///<summary>
157/// 得到分页数据
158///</summary>
159///<param name="connectionstring">连接字符串</param>
160///<returns>DataSet</returns>
161public DataSet QueryDataTable(string connectionstring)
162 {
163 SqlParameter[] parameters = {
164new SqlParameter("@Tables", SqlDbType.VarChar, 255),
165new SqlParameter("@PrimaryKey" , SqlDbType.VarChar , 255),
166new SqlParameter("@Sort", SqlDbType.VarChar , 255),
167new SqlParameter("@CurrentPage", SqlDbType.Int ),
168new SqlParameter("@PageSize", SqlDbType.Int ),
169new SqlParameter("@Fields", SqlDbType.VarChar, 255),
170new SqlParameter("@Filter", SqlDbType.VarChar, 1000),
171new SqlParameter("@Group" , SqlDbType.VarChar, 1000)
172 };
173 parameters[0].Value = _TableName;
174 parameters[1].Value = _PrimaryKey;
175 parameters[2].Value = _OrderStr;
176 parameters[3].Value = PageIndex;
177 parameters[4].Value = PageSize;
178 parameters[5].Value =_QueryFieldName;
179 parameters[6].Value = _QueryCondition;
180 parameters[7].Value =string.Empty;
181 ds =null;
182 ds =new DataSet();
183 ds = DbHelperSQL.RunProcedure(connectionstring, "uspDividePage", parameters, "tbPageData");
184
185if (_isQueryTotalCounts)
186 {
187 _TotalCount = GetTotalCount(connectionstring);
188 }
189
190if (_TotalCount ==0)
191 {
192 _PageIndex =0;
193 _PageCount =0;
194 }
195else
196 {
197 _PageCount = _TotalCount % _PageSize ==0? _TotalCount / _PageSize : _TotalCount / _PageSize +1;
198
199if (_PageIndex > _PageCount)
200 {
201 _PageIndex = _PageCount;
202 parameters[4].Value = _PageSize;
203 ds = QueryDataTable(connectionstring);
204 }
205 }
206
207return ds;
208 }
209
210///<summary>
211/// 得到总的记录数
212///</summary>
213///<param name="connectionstring">连接字符串</param>
214///<returns>总的记录数</returns>
215publicint GetTotalCount(string connectionstring)
216 {
217string strSql =" select count(1) from "+_TableName;
218
219if (_QueryCondition !=string.Empty)
220 {
221 strSql +=" where "+ _QueryCondition;
222 }
223
224return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString(), connectionstring));
225 }
226 }
227#endregion
窗体调用方法
一、设置窗体调用公共方法。
#region 绑定DataGridView
///<summary>
/// 绑定DataGridView
///</summary>
///<param name="sTb">表名</param>
///<param name="sPk">主键</param>
///<param name="sOrderField">排序字段</param>
///<param name="sWhere">查询条件</param>
///<param name="sQueryFieldName">字段列表</param>
///<returns>总记录数</returns>
privateint dgvBind(string sTb, string sPk, string sOrderField, string sWhere, string sQueryFieldName)
{
pageData =null;
dtPub =null;
pageData =new PageData();
dtPub =new DataTable();
pageData.TableName = sTb;
pageData.PrimaryKey = sPk;
pageData.OrderStr = sOrderField;
pageData.PageIndex =this.ucPageControlTest.PageCurrent;
pageData.PageSize =200;
pageData.QueryCondition = sWhere;
pageData.QueryFieldName = sQueryFieldName;
dtPub = pageData.QueryDataTable(ConfigurationSettings.AppSettings["DbConnection"]).Tables["tbPageData"];
this.ucPageControlTest.bindingSource.DataSource = dtPub;
this.ucPageControlTest.bindingNavigator.BindingSource = ucPageControlTest.bindingSource;
dgvUcPageControlTest.DataSource =null;
dgvUcPageControlTest.DataSource =this.ucPageControlTest.bindingSource;
if (dgvUcPageControlTest.Rows.Count >0)
{
dgvUcPageControlTest[4, ucPageControlTest.bindingSource.Position].Selected =true;
}
return pageData.TotalCount;
}
#endregion
二、在控件的EventPaging事件代码中调用即可。
return dgvBind("tbTestData", "UniqueID", "UniqueID", sQueryWhere, "*");
三、SqlServer测试数据代码如下:
-- =============================================
-- Author: EricHu QQ:80368704 WebSite:http://www.cnblogs.com/huyong/
-- Create date: 2010-12-18
-- Description: 原创企业级控件库之大数据量分页控件---测试数据
-- Modify Date: 2010-12-18
-- =============================================
/*一、创建数据库dbTest*/
CREATEDATABASE dbTest
go
/*二、创建数据表*/
USE[dbTest]
GO
CREATETABLE[dbo].[tbTestData](
[UniqueID][bigint]IDENTITY(20000,1) NOTNULL,
[CompanyName][varchar](200) COLLATE Chinese_PRC_CI_AS NULL,
[CompanyCode][varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Address][varchar](500) COLLATE Chinese_PRC_CI_AS NULL,
[Owner][varchar](100) COLLATE Chinese_PRC_CI_AS NULL,
[Memo][varchar](2000) COLLATE Chinese_PRC_CI_AS NULL,
[InsetDataTime][datetime]NULLCONSTRAINT[DF_tbTestData_InsetDataTime]DEFAULT (getdate()),
CONSTRAINT[PK_tbTestData]PRIMARYKEYCLUSTERED
(
[UniqueID]ASC
)WITH (IGNORE_DUP_KEY =OFF) ON[PRIMARY]
) ON[PRIMARY]
GO
/*三、增加测试数据*/
declare@countbigint
select@count=1
while@count<=5000000
begin
insertinto tbTestData
values('Company'+cast(@countasvarchar),'CompanyCode'+cast(@countasvarchar)
,'Address'+cast(@countasvarchar),'Owner'+cast(@countasvarchar)
,'Memo'+cast(@countasvarchar),getdate())
select@count=@count+1
end
下面给出本控件完整代码
1#region 版权信息
2/*---------------------------------------------------------------------*
3// Copyright (C) 2010 http://www.cnblogs.com/huyong
4// 版权所有。
5// 项目 名称:《Winform通用控件库》
6// 文 件 名: UcPageControl.cs
7// 类 全 名: DotNet.Controls.UcPageControl
8// 描 述: 分页控件
9// 创建 时间: 2010-06-05
10// 创建人信息: [**** 姓名:胡勇 QQ:80368704 E-Mail:80368704@qq.com *****]
11*----------------------------------------------------------------------*/
12#endregion
13
14using System;
15using System.Collections.Generic;
16using System.ComponentModel;
17using System.Drawing;
18using System.Data;
19using System.Data.SqlClient;
20using System.Windows.Forms;
21using DotNet.Common;
22using DotNet.DBUtility;
23
24namespace DotNet.Controls
25{
26#region 委托申明
27///<summary>
28/// 申明委托
29///</summary>
30///<param name="e"></param>
31///<returns></returns>
32publicdelegateint EventPagingHandler(EventPagingArg e);
33#endregion
34
35#region 分页控件
36///<summary>
37/// 分页控件
38///
39/// 修改纪录(此分页控件经过多次修改,已趋于完美,可放心使用。)
40/// 2010-12-06 胡勇 对上一条、下一条、首条、末条数据导航的隐藏,因为控件本身已做了处理。
41/// 2010-12-05 胡勇 对分页控件代码做了相应优化
42/// 2010-06-05 胡勇 创建分页控件
43///
44///<author>
45///<name>胡勇</name>
46///<QQ>80368704</QQ>
47///<Email>80368704@qq.com</Email>
48///</author>
49///</summary>
50 [ToolboxItem(true)]
51 [DefaultEvent("EventPaging")]
52 [ToolboxBitmap(typeof(UcPageControl), "DotNet.Controls.Images.UcPageControl.bmp")]
53 [Description("分页控件")]
54publicpartialclass UcPageControl : UserControl
55 {
56#region 申明事件
57///<summary>
58/// 单击移动到当前页上一末记录时发生
59///</summary>
60 [Category("数据分页"), Description("单击移动到当前页上一末记录时发生。"),Browsable(false)]
61publicevent EventHandler OnBindingNavigatorMovePreviousItemClick;
62
63///<summary>
64/// 单击移动到当前页第一条记录时发生
65///</summary>
66 [Category("数据分页"), Description("单击移动到当前页第一条记录时发生。"), Browsable(false)]
67publicevent EventHandler OnBindingNavigatorMoveFirstItemClick;
68
69///<summary>
70/// 单击移动到当前页下一条记录时发生
71///</summary>
72 [Category("数据分页"), Description("单击移动到当前页下一条记录时发生。"), Browsable(false)]
73publicevent EventHandler OnBindingNavigatorMoveNextItemClick;
74
75///<summary>
76/// 单击移动到当前页最后一条记录时发生
77///</summary>
78 [Category("数据分页"), Description("单击移动到当前页最后一条记录时发生。"), Browsable(false)]
79publicevent EventHandler OnBindingNavigatorMoveLastItemClick;
80
81///<summary>
82/// 单击各分页按钮(上一页、下一页、第一页、最后一页和转到某页)时发生
83///</summary>
84 [Category("数据分页"), Description("分页时发生。")]
85publicevent EventPagingHandler EventPaging;
86#endregion
87
88#region 构造函数
89public UcPageControl()
90 {
91 InitializeComponent();
92 }
93#endregion
94
95#region 属性
96
97privateint _pageSize =50; //每页显示记录数
98privateint _nMax =0; //总记录数
99privateint _pageCount =0; //页数=总记录数/每页显示记录数
100privateint _pageCurrent =0; //当前页号
101
102///<summary>
103/// 每页显示记录数
104///</summary>
105 [Category("数据分页"), Description("每页显示记录数。"), Browsable(false)]
106publicint PageSize
107 {
108get
109 {
110return _pageSize;
111 }
112set
113 {
114 _pageSize = value;
115 GetPageCount();//页数
116 }
117 }
118
119///<summary>
120/// 记录总数
121///</summary>
122 [Category("数据分页"), Description("记录总数。"),Browsable(false)]
123publicint NMax
124 {
125get
126 {
127return _nMax;
128 }
129set
130 {
131 _nMax = value;
132 GetPageCount();
133 }
134 }
135
136///<summary>
137/// 页数
138///</summary>
139 [Category("数据分页"), Description("页数。"), Browsable(false)]
140publicint PageCount
141 {
142get
143 {
144return _pageCount;
145 }
146set
147 {
148 _pageCount = value;
149 }
150 }
151
152///<summary>
153/// 当前页号
154///</summary>
155 [Category("数据分页"), Description("当前页号。"), Browsable(false)]
156publicint PageCurrent
157 {
158get
159 {
160return _pageCurrent;
161 }
162set
163 {
164 _pageCurrent = value;
165 }
166 }
167#endregion
168
169#region 方法
170 [Category("数据分页"), Description("bindingNavigator。"), Browsable(false)]
171public BindingNavigator ToolBar
172 {
173get
174 {
175returnthis.bindingNavigator;
176 }
177 }
178
179///<summary>
180/// 得到总页数
181///</summary>
182privatevoid GetPageCount()
183 {
184if (this.NMax >0)
185 {
186this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));
187 }
188else
189 {
190this.PageCount =0;
191 }
192 }
193
194///<summary>
195/// 绑定分页控件(关键代码)
196///</summary>
197publicvoid Bind()
198 {
199if (this.EventPaging !=null)
200 {
201this.NMax =this.EventPaging(new EventPagingArg(this.PageCurrent));
202 }
203
204if (this.PageCurrent >this.PageCount)
205 {
206this.PageCurrent =this.PageCount;
207 }
208if (this.PageCount ==1)
209 {
210this.PageCurrent =1;
211 }
212 lblPageCount.Text =this.PageCount.ToString();
213this.lblMaxPage.Text ="共"+this.NMax.ToString()+"条记录";
214this.txtCurrentPage.Text =this.PageCurrent.ToString();
215
216if (this.PageCurrent ==1)
217 {
218this.btnPrev.Enabled =false;
219this.btnFirst.Enabled =false;
220 }
221else
222 {
223 btnPrev.Enabled =true;
224 btnFirst.Enabled =true;
225 }
226
227if (this.PageCurrent ==this.PageCount)
228 {
229this.btnLast.Enabled =false;
230this.btnNext.Enabled =false;
231 }
232else
233 {
234 btnLast.Enabled =true;
235 btnNext.Enabled =true;
236 }
237
238if (this.NMax ==0)
239 {
240 btnNext.Enabled =false;
241 btnLast.Enabled =false;
242 btnFirst.Enabled =false;
243 btnPrev.Enabled =false;
244 }
245 }
246
247#endregion
248
249#region 按钮事件
250privatevoid btnFirst_Click(object sender, EventArgs e)
251 {
252 PageCurrent =1;
253this.Bind();
254 }
255
256privatevoid btnPrev_Click(object sender, EventArgs e)
257 {
258 PageCurrent -=1;
259if (PageCurrent <=0)
260 {
261 PageCurrent =1;
262 }
263this.Bind();
264 }
265
266privatevoid btnNext_Click(object sender, EventArgs e)
267 {
268this.PageCurrent +=1;
269if (PageCurrent > PageCount)
270 {
271 PageCurrent = PageCount;
272 }
273this.Bind();
274 }
275
276privatevoid btnLast_Click(object sender, EventArgs e)
277 {
278 PageCurrent = PageCount;
279this.Bind();
280 }
281
282privatevoid btnGo_Click(object sender, EventArgs e)
283 {
284if (this.txtCurrentPage.Text !=null&& txtCurrentPage.Text !="")
285 {
286if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent))
287 {
288this.Bind();
289 }
290else
291 {
292 DialogHelper.ShowErrorMsg("输入数字格式错误!");
293 }
294 }
295 }
296
297privatevoid txtCurrentPage_KeyDown(object sender, KeyEventArgs e)
298 {
299if (e.KeyCode == Keys.Enter)
300 {
301this.Bind();
302 }
303 }
304
305privatevoid bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
306 {
307if(OnBindingNavigatorMovePreviousItemClick !=null)
308 {
309 OnBindingNavigatorMovePreviousItemClick(this, null);
310 }
311 }
312
313privatevoid bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
314 {
315if (OnBindingNavigatorMoveFirstItemClick !=null)
316 {
317 OnBindingNavigatorMoveFirstItemClick(this, null);
318 }
319 }
320
321privatevoid bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
322 {
323if (OnBindingNavigatorMoveNextItemClick !=null)
324 {
325 OnBindingNavigatorMoveNextItemClick(this, null);
326 }
327 }
328
329privatevoid bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
330 {
331if (OnBindingNavigatorMoveLastItemClick !=null)
332 {
333 OnBindingNavigatorMoveLastItemClick(this, null);
334 }
335 }
336#endregion
337 }
338#endregion
339
340#region 自定义事件数据基类
341///<summary>
342/// 自定义事件数据基类
343///</summary>
344publicclass EventPagingArg : EventArgs
345 {
346privateint _intPageIndex;
347public EventPagingArg(int PageIndex)
348 {
349 _intPageIndex = PageIndex;
350 }
351 }
352#endregion
353
354#region 数据源提供(PageData)
355///<summary>
356/// 数据源提供
357///</summary>
358publicclass PageData
359 {
360 DataSet ds =null;
361privateint _PageSize =50; //分页大小
362privateint _PageIndex =1; //当前页
363privateint _PageCount =0; //总页数
364privateint _TotalCount =0; //总记录数
365privatestring _QueryFieldName ="*"; //表字段FieldStr
366privatebool _isQueryTotalCounts =true; //是否查询总的记录条数
367privatestring _TableName =string.Empty; //表名
368privatestring _OrderStr =string.Empty; //排序_SortStr
369privatestring _QueryCondition =string.Empty; //查询的条件 RowFilter
370privatestring _PrimaryKey =string.Empty; //主键
371
372///<summary>
373/// 是否查询总的记录条数
374///</summary>
375publicbool IsQueryTotalCounts
376 {
377get { return _isQueryTotalCounts; }
378set { _isQueryTotalCounts = value; }
379 }
380
381///<summary>
382/// 分页大小(每页显示多少条数据)
383///</summary>
384publicint PageSize
385 {
386get
387 {
388return _PageSize;
389
390 }
391set
392 {
393 _PageSize = value;
394 }
395 }
396
397///<summary>
398/// 当前页
399///</summary>
400publicint PageIndex
401 {
402get
403 {
404return _PageIndex;
405 }
406set
407 {
408 _PageIndex = value;
409 }
410 }
411
412///<summary>
413/// 总页数
414///</summary>
415publicint PageCount
416 {
417get
418 {
419return _PageCount;
420 }
421 }
422
423///<summary>
424/// 总记录数
425///</summary>
426publicint TotalCount
427 {
428get
429 {
430return _TotalCount;
431 }
432 }
433
434///<summary>
435/// 表名或视图名
436///</summary>
437publicstring TableName
438 {
439get
440 {
441return _TableName;
442 }
443set
444 {
445 _TableName = value;
446 }
447 }
448
449///<summary>
450/// 表字段FieldStr
451///</summary>
452publicstring QueryFieldName
453 {
454get
455 {
456return _QueryFieldName;
457 }
458set
459 {
460 _QueryFieldName = value;
461 }
462 }
463
464///<summary>
465/// 排序字段
466///</summary>
467publicstring OrderStr
468 {
469get
470 {
471return _OrderStr;
472 }
473set
474 {
475 _OrderStr = value;
476 }
477 }
478
479///<summary>
480/// 查询条件
481///</summary>
482publicstring QueryCondition
483 {
484get
485 {
486return _QueryCondition;
487 }
488set
489 {
490 _QueryCondition = value;
491 }
492 }
493
494///<summary>
495/// 主键
496///</summary>
497publicstring PrimaryKey
498 {
499get
500 {
501return _PrimaryKey;
502 }
503set
504 {
505 _PrimaryKey = value;
506 }
507 }
508
509///<summary>
510/// 得到分页数据
511///</summary>
512///<param name="connectionstring">连接字符串</param>
513///<returns>DataSet</returns>
514public DataSet QueryDataTable(string connectionstring)
515 {
516 SqlParameter[] parameters = {
517new SqlParameter("@Tables", SqlDbType.VarChar, 255),
518new SqlParameter("@PrimaryKey" , SqlDbType.VarChar , 255),
519new SqlParameter("@Sort", SqlDbType.VarChar , 255),
520new SqlParameter("@CurrentPage", SqlDbType.Int ),
521new SqlParameter("@PageSize", SqlDbType.Int ),
522new SqlParameter("@Fields", SqlDbType.VarChar, 255),
523new SqlParameter("@Filter", SqlDbType.VarChar, 1000),
524new SqlParameter("@Group" , SqlDbType.VarChar, 1000)
525 };
526 parameters[0].Value = _TableName;
527 parameters[1].Value = _PrimaryKey;
528 parameters[2].Value = _OrderStr;
529 parameters[3].Value = PageIndex;
530 parameters[4].Value = PageSize;
531 parameters[5].Value =_QueryFieldName;
532 parameters[6].Value = _QueryCondition;
533 parameters[7].Value =string.Empty;
534 ds =null;
535 ds =new DataSet();
536 ds = DbHelperSQL.RunProcedure(connectionstring, "uspDividePage", parameters, "tbPageData");
537
538if (_isQueryTotalCounts)
539 {
540 _TotalCount = GetTotalCount(connectionstring);
541 }
542
543if (_TotalCount ==0)
544 {
545 _PageIndex =0;
546 _PageCount =0;
547 }
548else
549 {
550 _PageCount = _TotalCount % _PageSize ==0? _TotalCount / _PageSize : _TotalCount / _PageSize +1;
551
552if (_PageIndex > _PageCount)
553 {
554 _PageIndex = _PageCount;
555 parameters[4].Value = _PageSize;
556 ds = QueryDataTable(connectionstring);
557 }
558 }
559
560return ds;
561 }
562
563///<summary>
564/// 得到总的记录数
565///</summary>
566///<param name="connectionstring">连接字符串</param>
567///<returns>总的记录数</returns>
568publicint GetTotalCount(string connectionstring)
569 {
570string strSql =" select count(1) from "+_TableName;
571
572if (_QueryCondition !=string.Empty)
573 {
574 strSql +=" where "+ _QueryCondition;
575 }
576
577return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString(), connectionstring));
578 }
579 }
580#endregion
581}
582