![]() | 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:添加数据 Insert方法 2:删除数据 Delete方法 3:更新数据 Update方法 一:添加操作 方法原型:
public bool Insert() public bool Insert(bool autoSetValue) public bool Insert(bool autoSetValue, InsertOption option) 示例1: using(MAction action = new MAction(TableNames.Users))
{
action.Set(Users.NickName, "路过秋天");//非UI型设值 action.UI.GetFrom(txtUserName)//UI型设值
if(action.Insert())
{
int id=action.Get<int>(Users.ID);//取回插入后的主键ID
} } 示例2:[通常只有action.Insert(true)一行] using(MAction action = new MAction(TableNames.Users))
{
action.Set(Users.NickName, "路过秋天");//非UI型设值
action.GetFrom(txtUserName)//UI型设值 action.UI.SetAutoPrefix("ppt");//设置控件前缀,可设置多个 action.Insert(true);//除了已赋值的,其它表字段,自动从Request["ppt字段"]中取值。 } 另:SetAutoPrefix 设置自动前缀方法(默认4个:空、txt、ddl、chb) 方法原形:public void SetAutoPrefix(string autoPrefix, params string[] otherPrefix) 可自定义设置多个前缀示例如: action.SetAutoPrefix("ppt","nud","lab"); 示例3: using(MAction action = new MAction(TableNames.Users))
{ return action.Insert(false,InsertOption.ID);//插入后,不需要取回自增加ID } InsertOption枚举说明: /// <summary>
/// 插入数据的返回值选项 /// </summary> public enum InsertOption { /// <summary> /// 使用此项:插入数据后[MSSQL会返回ID,其它数据库则不会返回ID] /// </summary> None, /// <summary> /// 使用此项:插入数据后会返回ID。 /// </summary> ID, /// <summary> /// 使用此项:插入数据后,会根据返回ID进行查询后填充数据行。[默认选项] /// </summary> Fill, } 二:删除操作 方法原型: public bool Delete() public bool Delete(object where) 示例1: using(MAction action = new MAction(TableNames.Users))
{ action.Delete(1); } 示例2: using(MAction action = new MAction(TableNames.Users))
{ action.Delete("1,2,3");//批量删除 } 三:更新操作 方法原型: public bool Update() public bool Update(object where) public bool Update(object where, bool AutoSetValue) 示例1: using(MAction action = new MAction(TableNames.Users))
{ action.Set(Users.Password, "http://cyq1162.cnblogs.com"); action.Update(1);//更新ID为1的密码 } 示例2: using(MAction action = new MAction(TableNames.Users))
{ action.Set(Users.Password, "http://cyq1162.cnblogs.com"); action.Update("UserName='路过秋天'"); } 示例3: using(MAction action = new MAction(TableNames.Users))
{
action.Set(Users.NickName, "路过秋天");//非UI型设值
action.Update("id=1",true);//除了已赋值的,其它表字段,自动从Request["字段"]中取值。 } ![]() |
游客[注册][163.179.46.*]2016/8/3 17:53:20 | #5 | |
![]() | 大神,如果我想通过一个对象(从数据映射过来的)要插入的话,我需要遍历字段然后每个set一下吗?有没有更好的方法?? |
游客[注册][219.131.183.*]2011/5/3 19:04:51 | #4 | |
![]() | 更正一下#3 是因为此方法只支持ASP.NET,不支持WinForm所至,并非程序出错,已得到管理员解答 reply: 已经支持Winform和WPF了。 只是设置前缀是另一个方法叫:SetAutoParentControl |
游客[注册][113.74.63.*]2011/4/28 12:02:56 | #3 | |
![]() | using(MAction action = new MAction("Emp")) { action.SetAutoPrefix("txt"); action.Insert(true); } 为什么这样执行不了?对应数据库有 3个字段:id,f1,f2,f3 文本框为:txtf1,txtf2,txtf3 执行时显示出错,如果用单独赋值,Set或GetFrom,则无问题 reply: 可以通过输出action.DebugInfo来查看具体执行的SQL语句,再进行判断。 |
allenliu2011/3/18 3:10:18 | #2 | |
![]() | 首先肯定Author的能力,开源这个框架也很有奉献的精神 个人感觉上面的 update 和 insert 还不够完善,不够灵活 reply: 这个教程写的比较早期,框架已经过多个版本的演进,有了不少的改进,具体可以看最新版本的API文档。 |
zhujiawei72011/3/2 22:54:02 | #1 | |
![]() | 请问我想insert类似于这样的语句该怎么调用。 insert 表A (字段1,字段2,字段3) select 字段10,字段11,getdate() from 表2 where 字段10=? reply: 如果你想执行sql语句,可以使用MProc,具体教程在后两篇中有介绍。 |
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.