CYQ.Data componentsCYQ.Data support multi-database application [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project |
文本数据库玩Code First
Technology exchange |
|
|
| #TopicOwner |
还记得,旧旧前发布过一篇文章:文本数据库--.Net界未来的一朵奇葩,经过多年的沉淀,在文本的操作上,已经有了些规模,因此,本文写出来和大伙分享一下。 一:Code First 简单操作: 2:解压就一个dll,项目添加引用即可。 3:既然是Code First,那我们就直接写实体类,示例如下: public class Users : CYQ.Data.Orm.OrmBase { public Users() { this.SetInit(this, "Users", "Txt Path={0}"); } public int ID {get;set;} public string UserName {get;set;} public string Password {get;set;} } 说明两个点: 1,继承自OrmBase; 2,构造函数里指定表名“Users”和数据库链接(Txt Path={0})。 数据库链接说明({0}代表根目录): 文本数据库(json存储方式):Txt Path={0} Xml数据库(xml存储方式) :Xml Path={0} 其它数据库,就是各数据库的链接了或配置项了。 OK,现在就可以实操增删改查了,下面轻轻的讲一下操作实例和原理。 二:Code First 实操 1:插入与更新代码和图如下: 代码说明: 左侧是增加和更新的代码,右侧是一简单的示例操作winform界面,这里有两重点说明。 1:为啥实体类要用using包含操作? 对于文本的操作,内部机制是在内存操作的,直到调用Disponse,才写到文本,因此,用using就是为了结束时调用Disponse写到文件去的。 2:SetAutoParentControl是什么东东? 当控件名按约束起名:为3字母+属性名,如txtUserName,txtPassword时,可以采用自动取值法, 这样你拉N个控件上去,代码也不用改变的。 传进this就是form本身,orm会遍历form下的控件进行取值。 2:对于其它Orm功能,这里不详写了,下载里有api文档,这里仅列出接口: /// <summary> /// 数据操作公共接口 /// </summary> /// <typeparam name="T"></typeparam> internal interface ICommon:IDisposable { bool Insert(); bool Insert(InsertOp option); bool Insert(bool autoSetValue); bool Insert(bool autoSetValue, InsertOp option); bool Update(); bool Update(object where); bool Update(object where, bool autoSetValue); bool Delete(); bool Delete(object where); bool Fill(object where); MDataTable Select(); MDataTable Select(string where); MDataTable Select(int topN, object where); MDataTable Select(int pageIndex, int pageSize, string where, out int count); int GetCount(string where); bool Exists(string where); #region UI接口 void GetFrom(object control); void GetFrom(object control, object value); bool GetFromJson(string jsonOrFileName); void SetAutoParentControl(object parent, params object[] otherParent); void SetAutoPrefix(string autoPrefix, params string[] otherPrefix); void SetSelectColumns(params object[] columnNames); void SetTo(object control); void SetTo(object control, object value, bool isControlEnabled); void SetTo(object control, object value); void SetToAll(params object[] parentControls); #endregion string TableName { get; set; } string DebugInfo { get; } } 三:基础说明解答 1:表从哪来、数据存哪? 系统在初始化时,会根据表名和链接的地址,会根据属性名称自动生成相应的表结构在地址目录,并且以json或xml方式存储数据。 2:where支持sql? 支持的,不过对于txt,xml,内置实现了最基础的sql解析,可以满足>,>=,<,<=,=,is null,is not null,like,order by 等基本本询,不支持的有:函数,或group by等语句。 3:实用场景 有时候,数据并不大,用access太纠心,用sqlite服务器安全设置又不允许,用mssql又觉得杀牛不用牛刀,那txt,xml就该出手了。 场景一:比如一简单的配置文件,你写个实体类,就可以轻松的操作了,不用建啥数据库表 。 场景二:Code First的目的,当你需要快速构建代码时,直接就用txt就上了,然后业务代码就可以开始写了,不用等DBA建完库。 后期可根据需要,修改web.config的链接转到mssql,mysql,oracle等数据库,业务代码不变的哟。 当然了,this.SetInit(this, "Users", "Conn");应该这么写,对应webconfig的Conn配置项。 场景三:对于wp7开发,直接操作txt,还是很合理的。 4:多数据库兼容性 用过CYQ.Data的朋友都知道,支持多数据库是非常轻松过度的,现在你玩txt,轻松换下链接就到mssql等其它数据库了,所以玩好一个,其它数据库同样是兼容的。 感兴趣的朋友,可以玩一玩,后续再上一个通用型的增删改查示例。 |
柚子[159.226.100.*]2012/10/13 19:33:57 | #2 | |
不错,好东西。继续关注 |
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.