![]() | CYQ.Data componentsCYQ.Data support multi-database application [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project |
CYQ.Data 数据框架 使用篇二 MAction 数据查询
Guide |
|
|
| #TopicOwner |
本节内容:[带*号内容为新版本功能函数,旧版本可能没有该功能] 本篇继续上一篇内容,本节介绍所有相关查询的使用。
主要内容提要: 1:单行数据操作 Fill 操作 GetCount、Exists(*)操作。 2:多行数据操作 Select 操作 3:列表绑定控件操作 配合分页控件 4:多表查询及绑定 视图及自定义SQL 单行数据操作 一:Fill 填充方法,单行查询 方法原形:public bool Fill(object where) 示例1:直传ID using(MAction action = new MAction(TableNames.Users))
{
if (action.Fill(888))//查询主键=888的单行数据 { action.UI.SetToAll(this); } } 示例2:传复杂的where条件 using(MAction action = new MAction(TableNames.Users))
{
if (action.Fill("id>888 or UserName='路过秋天'"))//查询ID>888或用户名为"路过秋天"的单行数据 { action.UI.SetToAll(this); } } 示例3:where条件附带order by using(MAction action = new MAction(TableNames.Users))
{
if (action.Fill("id>888 order by id desc"))//查询ID>888的结果中取ID最大的的单行数据 { action.UI.SetToAll(this); } } 示例4:[MDataRow]行数据转实体 using(MAction action = new MAction(TableNames.Users))
{ }if (action.Fill(888))//查询ID>888的结果中取ID最大的的单行数据 { UserInfo info=action.Data.ToEntity<UserInfo>();//UserInfo为实体类。 } 二:GetCount 取统计总数 方法原形:public int GetCount(string where)
示例(若取总数据,参数可传空或Null): using(MAction action = new MAction(TableNames.Users))
{ int count=action.GetCount("id>10"); } 三:Exists 是否存在指定条件的数据(*) 方法原形:public int Exists(string where) 示例: using(MAction action = new MAction(TableNames.Users))
{ bool userExists=action.Exists("路过秋天"); // 取唯一键(或除了主键开始的第一个字符串字段;智能推导为:UserName='路过秋天' }
多行数据操作 三:Select 多数据查询 方法原形:
1:public MDataTable Select() 2:public MDataTable Select(string where)(*) 3:public MDataTable Select(int PageIndex, int PageSize, string Where, out int RowCount) 示例1: using(MAction action = new MAction(TableNames.Users))
{ MDataTable tabme = action.Select();//查询所有数据 } 示例2: using(MAction action = new MAction(TableNames.Users))
{ MDataTable tabme = action.Select("id>10 order by id desc");//查询指定条件的所有数据并降序排列 } 示例3: int count;//这个为返回的记录总数
using(MAction action = new MAction(TableNames.Users))
{
MDataTable tabme = action.Select(1,10,"id>10 order by id desc",out count); //查询id>10的10条记录[第1页,每页10条数据,结果按usename排序] }
列表绑定操作 四:绑定GridView/DataList/Repeater 示例1:查询所有直接绑定 using(MAction action = new MAction(TableNames.Users))
{ action.Select().Bind(gvUsers); } 示例2:配合 分页控件绑定 [下载地址:http://www.cyqdata.cn/download/article-detail-28683] public void BindData()
{ int count; using(MAction action = new MAction(TableNames.Users))
{
action.Select(Pager1.PageIndex,Pager1.PageSize,"id>10 order by id desc",out count).Bind(gvUsers); } Pager1.Count = count;//设置记录总数
Pager1.BindName = "BindData";//绑定方法名称,需要为Public }
多表查询及绑定 五:视图方式 示例1:和表操作一样,唯一区别就是表名换成视图名称 using(MAction action = new MAction(ViewNames.V_Users))
{ MDataTable tabme = action.Select();//查询所有数据 }
六:多表查询:自定义构造多表SQL语句 示例1: public void BindData()
{ string customTable = "select u.*,m.Body from Users u left join Message m on u.ID=m.UserID";
int count; using(MAction action = new MAction(customTable))
{
action.Select(Pager1.PageIndex,Pager1.PageSize,"id>10 order by id desc",out count).Bind(gvUsers); } Pager1.Count = count;//设置记录总数
Pager1.BindName = "BindData";//绑定方法名称,需要为Public } 说明: 在具体使用过程中,为了方便管理,直接出现在自定义SQL语句就不这样直接写在界面中了,可以新项建一个项目统一管理自定义的SQL。 English version of the link:http://www.cyqdata.cn/cyqdata/article-detail-41133 基础教程: 其它教程:
![]() |
游客[注册][180.117.132.*]2016/8/25 14:37:47 | #13 | |
![]() | 查询语句有点问题,软件启动时查询语句可以从数据库查询出数据,软件一直运行时无论怎么修改数据库,查询出来的还是老数据,不知道是为什么 |
游客[注册][180.117.133.*]2016/8/20 15:06:01 | #12 | |
![]() | 我语句中用到了union all而且两个查询都有查询条件,action.select总是不成功,不知道有没有什么好的解决办法,谢谢 |
游客[注册][183.38.233.*]2016/7/14 10:36:00 | #11 | |
![]() | 真心好用,想问下秋天直接拼写sql怕注入吗 |
游客[注册][58.213.137.*]2015/4/15 12:05:27 | #10 | |
![]() | 楼主,,从数据库里查出来并绑定datagridview,但是显示的都是数据库里的英文名,怎么改??好纠结啊这个。。。。 reply: 这个Web或Win都有提供编辑表格,添加自定义列(指定标题与绑定字段的) |
游客[注册][113.111.120.*]2011/7/9 12:27:31 | #9 | |
![]() | 帮5楼补充一下: bool userExists=action.Exists("UserName='路过秋天'"); 如果改成 bool userExists=action.Exists("UserName='"+txtName.Text+"'"); 然后人家被人写入 abc' or '1'='1 会否被注入 reply: 不会。 |
游客[注册][211.160.165.*]2011/5/17 23:59:32 | #7 | |
![]() | “你要做的是好好学习,等你有写框架的能力再来看你这条回复。” 我不会做菜就不能说好吃不好吃了,就不能拒绝吃了,你这个逻辑有点神奇啊 reply: 你没吃菜你就在说菜不好吃,你的逻辑比较神奇些。 |
游客[注册][221.5.67.*]2011/5/17 10:12:46 | #6 | |
![]() | 拼SQL看起来很不爽啊。 为什么不直接用SQL? 如果ORM做的彻底一点那就用linq好了。 reply: 你要做的是好好学习,等你有写框架的能力再来看你这条回复。 |
游客[注册][210.13.114.*]2011/5/8 21:16:14 | #5 | |
![]() | 拼sql不怕注入? reply: 底层有强大的过滤函数,你可以自己试一下能不能注入。 |
游客[注册][120.81.227.*]2011/3/22 8:57:39 | #4 | |
![]() | 秋天,你就是我心目中的神啊,看了你的CYQ.Data框架后,真是令我佩服的五体投地,继续努力学习中…… reply: :) 欢迎经常提出反馈与建议。 |
游客[注册][202.96.1.*]2011/3/21 22:33:07 | #3 | |
![]() | 还是linq的查询方式更好玩些 reply: 站在门外,怎么能看到里面的风景呢? |
Anonymous[Register][222.65.99.*]2011/3/21 5:28:17 | #2 | |
![]() | 冏我还是自己写SQL吧。。。 reply: 看你没注册用户,连学都这么不认真。 |
游客[注册][125.39.108.*]2011/3/7 5:34:42 | #1 | |
![]() | 字符串 查询 方式还是用的不顺, 不流畅..||| reply: 哪里不顺呢? |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.