![]() | 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.Orm.DBFast 新增类介绍
Principles |
|
|
| #TopicOwner |
前言:以下功能在国庆期就完成并提前发布了,但到今天才有时间写文介绍,主要是国庆后还是选择就职了,悲催的是上班的地方全公司都能上网,唯独开发部竟不让上网,是个局域网。 也不是全不能上,房间里有三台能上网的机子(两台笔记本+一台台式机),下载资料还得用公司的U盘再转到自己电脑,这种半封闭的环境,相当的让人不适应,有种欲仰天吐血的感觉。 这一周我都向三个带总的领导反映了上网问题,不过没啥响应,估计是没戏。 于是我只有把其中一台能上网的笔记本拿到自己桌子上去独自占用了,勉强也能上下网了,不过基于安全问题,我也不好在那机子里登陆私人账号。 经过一周的研究,我发现解决方案还是有的:因为公司的规定只是开发的机子不让上网而已,自己按理应该可以带笔记本去上网,不过我奇怪的是竟然整个部门都没人带笔记本去,不知道搞啥名头。 好了,废话不多说了,下面入文章的正题: CYQ.Data V5 配置工具:最新更新了配置工具,新的界面截图如下: 本次调整的功能如下: 1:编码模式:新增加纯实体的生成: 至此,CYQ.Data 就有了三种编码模式,分别是: A: 枚举型(MAction、MProc)- 性能最优的编码模式 B: 实体型(充血型的ORM操作 - 需要实体类继承CYQ.Data.Orm.OrmBase,也支持CodeFirst模式) C: 纯实体(贫血型的ORM操作 - 通过本次新增的CYQ.Data.Orm.DBFast 静态类来操作) 2:生成的实体带说明文字。 3:默认名称空间增加{0}来代表数据库名称。 4:多数据库模式下,默认的数据库链接,约定对应的Web.Config的配置为"数据库名称Conn“。 下面示例生成一个实体类如下: using System;
namespace Web.Entity.Demo { public class Users { /// <summary> /// 标识ID /// </summary> public int? ID { get; set; } /// <summary> /// 用户名 /// </summary> public string UserName { get; set; } /// <summary> /// 创建日期 /// </summary> public DateTime? CreateTime { get; set; } } } 对于这样的实体,默认的数据库链接就是: <add name="DemoConn" connectionString="server=.;database=demo;uid=sa;pwd=123456"/>
如果DemoConn不存在,则会取默认的Conn项。 工具的源码下载地址:http://www.cyqdata.cn/download/article-detail-426
CYQ.Data.Orm.DBFast 类介绍这是我最近新增加的静态类,主要是为了节省点代码,方便一些基础的操作。 示例代码(以上面的的Users实体为示例): 查询实体: Users u=DBFast.Find<Users>(1);//一行查一个实体。 List<Users> uList=DBFast.Select<Users>(2,10,"id>10");//分页查询满足条件的列表。 增加数据: DBFast.Insert<Users>(new Users{UserName="a";}); 更新数据: DBFast.Update<Users>(new Users{UserName="a";},1); 删除数据: DBFast.Delete<Users>(1); 以上就是简的操作,增加这个静态类的意图,是为了简化一些常规的操作,让一行代码去解决,减少代码量。 所以这个静态类并不是万能的,其它复杂性的的操作方式, 建议使用枚举型的常规操作。 下面提供这个DBFast静态类的源码: using System;
using System.Collections.Generic; using System.Text; using CYQ.Data.Table; namespace CYQ.Data.Orm { /// <summary> /// 快速操作操作类。 /// </summary> public static class DBFast { /// <summary> /// 查找单条记录 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="where">条件</param> /// <param name="columns">指定查询的列(可选)</param> /// <returns></returns> public static T Find<T>(object where, params string[] columns) { T result = default(T); MDataRow row = null; using (MAction action = GetMAction<T>()) { if (columns != null && columns.Length > 0) { action.SetSelectColumns(columns); } if (action.Fill(where)) { row = action.Data; } } if (row != null) { result = row.ToEntity<T>(); } return result; } public static List<T> Select<T>() { int count; return Select<T>(0, 0, null, out count, null); } /// <summary> /// 列表查询 /// </summary> /// <param name="where">查询条件[可附带 order by 语句]</param> /// <returns></returns> public static List<T> Select<T>(string where, params string[] columns) { int count; return Select<T>(0, 0, where, out count, columns); } /// <summary> /// 列表查询 /// </summary> /// <param name="topN">查询几条</param> /// <param name="where">查询条件[可附带 order by 语句]</param> /// <returns></returns> public static List<T> Select<T>(int topN, string where, params string[] columns) { int count; return Select<T>(1, topN, where, out count, columns); } public static List<T> Select<T>(int pageIndex, int pageSize, params string[] columns) { int count; return Select<T>(pageIndex, pageSize, null, out count, columns); } public static List<T> Select<T>(int pageIndex, int pageSize, string where, params string[] columns) { int count; return Select<T>(pageIndex, pageSize, where, out count, columns); } /// <summary> /// 查找多条记录 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="pageIndex">第N页</param> /// <param name="pageSize">每页N条</param> /// <param name="where">条件</param> /// <param name="count">返回记录总数</param> /// <param name="columns">指定查询的列(可选)</param> /// <returns></returns> public static List<T> Select<T>(int pageIndex, int pageSize, object where, out int count, params string[] columns) { MDataTable dt = null; using (MAction action = GetMAction<T>()) { if (columns != null && columns.Length > 0) { action.SetSelectColumns(columns); } dt = action.Select(pageIndex, pageSize, where, out count); } return dt.ToList<T>(); } /// <summary> /// 删除记录 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="where">条件</param> /// <returns></returns> public static bool Delete<T>(object where) { bool result = false; using (MAction action = GetMAction<T>()) { result = action.Delete(where); } return result; } public static bool Insert<T>(T t) { return Insert<T>(t, InsertOp.ID); } /// <summary> /// 添加一条记录 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="t">实体对象</param> /// <returns></returns> public static bool Insert<T>(T t, InsertOp op) { bool result = false; MDataRow row = null; using (MAction action = GetMAction<T>()) { action.Data.SetFromEntity(t); result = action.Insert(op); if (op != InsertOp.None) { row = action.Data; } } if (row != null) { row.SetToEntity(t); } return result; } public static bool Update<T>(T t) { return Update<T>(t, null); } /// <summary> /// 更新记录 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="t">实体对象</param> /// <param name="where">条件</param> /// <returns></returns> public static bool Update<T>(T t, object where) { bool result = false; using (MAction action = GetMAction<T>()) { action.Data.SetFromEntity(t); result = action.Update(where); } return result; } private static MAction GetMAction<T>() { string conn = string.Empty; MAction action = new MAction(GetTableName<T>(out conn), conn); action.SetNoAop(); return action; } private static string GetTableName<T>(out string conn) { conn = string.Empty; Type t = typeof(T); string[] items = t.FullName.Split('.'); if (items.Length > 1) { conn = items[items.Length - 2] + "Conn"; items = null; } string tName = t.Name; t = null; return tName; } } } ![]() |
游客[注册][60.190.25.*]2013/12/23 21:10:41 | #1 | |
![]() | 不知道算不算bug。 比如class Parent,有属性Name,class Child : Parent 继承。 DBFast.Find<Child>(name),返回child.Name = null。 当Child重写Name属性后,返回child.Name = name。 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.