回首往事,碩果累累,展望未來,信心滿懷。
Where is way? Pick One,Not All.
博客园
首页
社区
新随笔
联系
订阅
管理
随笔-96 评论-55 文章-0 trackbacks-5
手写分页函数C#
/**/
///
<summary>
///
返回数据集
///
</summary>
///
<param name="sql"></param>
///
<returns></returns>
public
DataSet GetData(
string
sql,
string
strconn)
{
OleDbConnection ole
=
new
OleDbConnection(
"
PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=
"
+
strconn);
ole.Open();
OleDbCommand cm
=
new
OleDbCommand(sql,ole);
DataSet ds
=
new
DataSet();
OleDbDataAdapter da
=
new
OleDbDataAdapter(cm);
da.Fill(ds);
ole.Close();
return
ds;
}
/**/
///
<summary>
///
分页
///
</summary>
///
<param name="pagesize">
每页大小
</param>
///
<param name="pageindex">
当前页数
</param>
///
<param name="field">
获取字段名
</param>
///
<param name="tablename">
表名
</param>
///
<param name="orderfield">
排序字段
</param>
///
<param name="taxis">
排序方式,true为升序,false为降序
</param>
///
<param name="condition">
满足的条件
</param>
///
<returns></returns>
public
DataSet GetPage(
int
pagesize,
int
pageindex,
string
field,
string
tablename,
string
orderfield,
bool
taxis,
string
condition)
{
string
temp;
if
(taxis)
{
temp
=
"
asc
"
;
}
else
{
temp
=
"
desc
"
;
}
string
sql;
if
(pageindex
==
1
)
{
if
(condition
==
""
)
{
sql
=
"
select top
"
+
pagesize
+
"
"
+
field
+
"
from
"
+
tablename
+
"
order by
"
+
orderfield
+
"
"
+
temp;
return
GetData(sql);
}
else
{
sql
=
"
select top
"
+
pagesize
+
"
"
+
field
+
"
from
"
+
tablename
+
"
where
"
+
condition
+
"
order by
"
+
orderfield
+
"
"
+
temp;
return
GetData(sql);
}
}
else
{
pageindex
=
(pageindex
-
1
)
*
pagesize;
if
(condition
==
""
)
{
if
(taxis)
{
sql
=
"
select top
"
+
pagesize
+
"
"
+
field
+
"
from
"
+
tablename
+
"
where
"
+
orderfield
+
"
>all(select top
"
+
pageindex
+
"
"
+
orderfield
+
"
from
"
+
tablename
+
"
order by
"
+
orderfield
+
"
"
+
temp
+
"
) order by
"
+
orderfield
+
"
"
+
temp;
}
else
{
sql
=
"
select top
"
+
pagesize
+
"
"
+
field
+
"
from
"
+
tablename
+
"
where
"
+
orderfield
+
"
<all(select top
"
+
pageindex
+
"
"
+
orderfield
+
"
from
"
+
tablename
+
"
order by
"
+
orderfield
+
"
"
+
temp
+
"
) order by
"
+
orderfield
+
"
"
+
temp;
}
return
GetData(sql);
}
else
{
if
(taxis)
{
sql
=
"
select top
"
+
pagesize
+
"
"
+
field
+
"
from
"
+
tablename
+
"
where
"
+
condition
+
"
and
"
+
orderfield
+
"
>all(select top
"
+
pageindex
+
"
"
+
orderfield
+
"
from
"
+
tablename
+
"
where
"
+
condition
+
"
order by
"
+
orderfield
+
"
"
+
temp
+
"
) order by
"
+
orderfield
+
"
"
+
temp;
}
else
{
sql
=
"
select top
"
+
pagesize
+
"
"
+
field
+
"
from
"
+
tablename
+
"
where
"
+
condition
+
"
and
"
+
orderfield
+
"
<all(select top
"
+
pageindex
+
"
"
+
orderfield
+
"
from
"
+
tablename
+
"
where
"
+
condition
+
"
order by
"
+
orderfield
+
"
"
+
temp
+
"
) order by
"
+
orderfield
+
"
"
+
temp;
}
return
GetData(sql);
}
}
}
希望给于评价
绿色通道:
好文要顶
关注我
收藏该文
与我联系
posted on 2006-12-08 15:36
感動常在
阅读(1365)
评论(4)
编辑
收藏
评论:
1477305
#1楼
2006-12-08 17:44
|
一滴水
分页可以看看这篇:
http://tonyqus.cnblogs.com/archive/2006/03/12/344357.html
尽量不要使用字符串拼凑sql,建议使用参数
回复
引用
查看
#2楼
2006-12-11 15:02
|
旋律游魂
太多字符串连接,性能低,用StringBulider来代替。
回复
引用
查看
#3楼
2007-01-01 22:35
|
过路人[未注册用户]
字符串拼凑太多,建议使用高级SQL中的分页查询功能!
回复
引用
#4楼
2009-03-15 14:22
|
学习分页的游客[未注册用户]
确实是。其实你可以把参数都做好处理再把字符串接好。。。
接好以后。最后再用一句SQL来执行。
以前ASP这样做的。。现在学.net刚好看到这篇文章
回复
引用
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
《福布斯》:谷歌进军硬件产品 难撼动苹果地位
·
美国空军拟最多购买1.8万台iPad 2
·
伊朗封杀Gmail和Facebook等互联网服务
·
分析称专利之争让谷歌苹果两败俱伤
·
Android平台发现新型手机病毒Rootsmart
»
更多新闻...
最新知识库文章
:
·
高级编程语言的发展历程
·
如何学习一门新的编程语言?
·
学习不同编程语言的重要性
·
为什么我喜欢富于表达性的编程语言
·
计算机专业的女生为什么要学编程
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
QQ:1149015928
昵称:
感動常在
园龄:
5年6个月
粉丝:
9
关注:
1
<
2006年12月
>
日
一
二
三
四
五
六
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
我的标签
.net
(7)
linux
(6)
ruby
(4)
读书
(4)
php
(3)
javascript
(2)
soft
(1)
算法
(1)
随笔分类
dotnet心得(37)
JavaScript技巧(7)
linux(6)
PHP(7)
Ruby on Rails(4)
編程之美(4)
查找錯誤(1)
数据库心得(10)
文件操作(4)
我的作品(3)
想到什么,说什么(2)
有用的資源文件(7)
随笔档案
2010年9月 (1)
2010年8月 (1)
2010年7月 (1)
2010年5月 (3)
2010年3月 (1)
2009年9月 (4)
2009年3月 (1)
2009年2月 (5)
2009年1月 (2)
2008年11月 (3)
2008年10月 (1)
2008年9月 (3)
2008年8月 (3)
2008年6月 (1)
2008年5月 (1)
2008年4月 (3)
2008年3月 (1)
2008年2月 (8)
2008年1月 (8)
2007年10月 (1)
2007年9月 (3)
2007年8月 (8)
2007年7月 (9)
2007年6月 (2)
2007年5月 (1)
2007年2月 (1)
2007年1月 (3)
2006年12月 (3)
2006年11月 (1)
2006年10月 (5)
2006年9月 (1)
2006年8月 (7)
最新评论
阅读排行榜
评论排行榜
推荐排行榜