![]() | 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 V5 MAction新增加SetExpression方法说明
Guide |
|
|
| #TopicOwner |
需求: 在之前的CYQ.Data版本中,如果你要更新某一个字段的值+1; 解决方案: 1:如果用MAction操作,可能会如下代码: using(MAction action=new MAction(表名)) { if(action.Fill(id)) { action.Set(字段名,action.Get<int>(字段名)+1); action.Update(); } } 简单的说,就是需要查询出值,然后再用原值进行相关的操作,用这种代码代价就是要多查一次。 2:另一种方式高性能的是使用MProc来直接操作Sql语句,可能会如下代码: using(MProc proc=new Proc("Update 表名 set 字段名=字段名+1")) { proc.ExeNonQuery(); } 用MProc基本是万能的了,直接处理SQL语句和存储过程。。。 大伙习惯了MAction多,所以希望MAction也有个顺路功能: 不过好多人习惯用MAction,都图个方便,连条SQL语句也想省一下。。。 事实上,我本人在多个项目中,也有这种需求,当然最后都是直接使用MProc处理了。 很多时候,我一直在寻找一种最优方式,能处理这种值+1,值-1的问题,不过每每看着架构,不好下手,一直没找到切入点和使用方法。 比如一开始想的: using(MAction action=new MAction(表名)) { action.Set(字段名,“[字段名]+1”); action.Update("id=1"); } 但是这种方式,实现起来,没想象的简单,而且这值是字符串还是表达式?分不清也说不明,所以一直卡在以何种使用方式上。 CYQ.Data 框架设计原则: CYQ.Data 框架的设计,很多时候,是先思考使用的方法,再去想怎么实现的,先想象出最简单的使用方式,再去想能不能实现,而不是实现了一个功能,却很难使用。 为此,但网友又对我提出这种需求的要求时,我又花了N个小时,看着代码发呆,每次我思考问题或有某项功能时,我都会看着代码发呆几个小时。。当然是在不停的思考。。 后来终于设计出最终解决方案: using(MAction action=new MAction(表名)) { action.Set(字段名,值); action.SetExpression(“字段名=字段名+1”); action.SetExpression(“字段名=len(字段名)+1”); action.Update(); } 通过增加一个表达式方法,基本上有点万能了,事实在后端,组成的Update语句大体为: update 表名 set 表达式,字段名=@字段名 where ... 简单说,就是表达式只是简单的被附加到原来的set的后面,最终被组装成SQL执行,当然了,如果你想高级点用参数化也是可以的。 using (MAction action = new MAction("Blog_Class")) { action.SetExpression("count=count+@Value");//表达式升级带参数化。 action.SetPara("Value", 2, DbType.Int32);//设置参数化值 action.Update(); } 到此,基本上有表达存在,可以处理N种不同的需求了,方便性提升了不少。 ![]() |
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.