Simple.Data官方地址: http://simplefx.org/simpledata/docs/index.html
这是一款轻量级别,易用的ORM工具。基于.net framework 4.0的Dynamic特性,支持多种数据库:
- SQL Server 2005 and later
- SQL Server Compact Edition 4.0
- oracle
- vistadb
- mysql 4.0 and later
- sqlite 3.0 and later
- PostgreSQL
- sqlanywhere
- informix
- Microsoft Access 2000, 2003, and 2007
- mongodb
- OData
- SQL Azure Table storage
用过的朋友都知道好用,所以这里就不多介绍了,下面直接贴上我的代码。
using Simple.Data; namespace SimpleDataDemo.Helper { public class DataBaseHelper { private const string Con = "Server=(local);Initial Catalog=Blogging;User ID=sa;Password=12345678;"; public static dynamic Open() { return Database.Opener.OpenConnection(Con); } } }
using System.Collections.Generic; using System.Linq; using Simple.Data; namespace INetFang.Helper { /// <summary> /// Simple Data操作通用类 作者:Fang /// http://simplefx.org/simpledata/docs/index.html /// </summary> /// <typeparam name="T">Table Model</typeparam> public class SimpleManager<T> { private readonly dynamic _db = DataBaseHelper.Open(); private string TableName { get; set; } public SimpleManager() { TableName = typeof (T).Name; } /// <summary> /// 表的名称 /// </summary> /// <param name="tableName"></param> public SimpleManager(string tableName) { TableName = tableName; } public dynamic Db() { return _db[TableName]; } #region Exists /// <summary> /// 判断是否存在 /// </summary> /// <param name="column"></param> /// <param name="value"></param> /// <returns></returns> public bool Exists(string column, dynamic value) { return Db().Exists(Db()[column] == value); } /// <summary> /// 例子: /// var e1 = new SimpleExpression(Db()["Id"], 5, SimpleExpressionType.Equal); /// var e2 = new SimpleExpression(Db()["Url"], "2", SimpleExpressionType.Equal); /// bool exi = Exists(e1 && e2) /// /// bool exi = Exists(Db()["Id"]==5 && Db()["Url"]==2) /// </summary> /// <param name="criteria">表达式</param> /// <returns></returns> public bool Exists(SimpleExpression criteria) { return Db().Exists(criteria); } #endregion #region Insert /// <summary> /// 增加 /// </summary> /// <param name="model">实体对象</param> /// <returns></returns> public T Insert(T model) { return Db().Insert(model); } /// <summary> /// 批量增加 /// </summary> /// <param name="models">实体对象集合</param> /// <returns></returns> public List<T> Insert(List<T> models) { return Db().Insert(models).ToList<T>(); } #endregion #region Update /// <summary> /// 根据主键修改 /// </summary> /// <param name="model">实体对象</param> /// <returns>bool值,判断是否操作成功</returns> public bool Update(T model) { return Db().Update(model) > 0; } #endregion #region Delete /// <summary> /// 删除 /// </summary> /// <param name="id">Id</param> /// <returns>bool值,判断是否操作成功</returns> public bool Delete(dynamic id) { return Db().DeleteById(id) > 0; } /// <summary> /// 批量删除 /// </summary> /// <param name="ids">Id集合</param> /// <returns>bool值,判断是否操作成功</returns> public bool DeleteBatch(dynamic ids) { return Db().DeleteById(ids) > 0; } public bool DeleteBatch(string idstring) { return Db().DeleteById(idstring.Split(',').Where(str => !string.IsNullOrEmpty(str)).Cast<dynamic>().ToArray()) > 0; } /// <summary> /// 删除全部 /// </summary> /// <returns>bool值,判断是否操作成功</returns> public bool DeleteAll() { return Db().DeleteAll() > 0; } #endregion #region Find /// <summary> /// 获取最大值 /// </summary> /// <param name="column">列名</param> /// <returns></returns> public int GetMaxBy(string column) { return Db().Select(Db()[column].Max()).ToScalarOrDefault(); } /// <summary> /// 记录数量 /// </summary> /// <returns></returns> public int GetCount() { return Db().GetCount(); } public dynamic GetCount(SimpleExpression strWhere) { return Db().GetCount(strWhere); } /// <summary> /// 查询全部 /// </summary> /// <returns>实体类对象集合</returns> public List<T> FindAll() { return Db().All().ToList<T>(); } /// <summary> /// 通过Id查询 /// </summary> /// <param name="id">主键Id</param> /// <returns>实体类对象集合</returns> public List<T> FindAllById(dynamic id) { return Db().FindAllById(id); } /// <summary> /// 通过Id查询 /// </summary> /// <param name="id">主键Id</param> /// <returns>实体类对象</returns> public T FindById(dynamic id) { return Db().FindAllById(id).FirstOrDefault(); } /// <summary> /// 通过表达式查询 /// </summary> /// <param name="criteria">表达式</param> /// <returns>实体类对象集合</returns> public List<T> FindAll(SimpleExpression criteria) { return Db().FindAll(criteria).ToList<T>(); } public List<T> FindAll(SimpleExpression criteria, string filedOrder) { return Db() .FindAll(criteria) .OrderBy(Db()[filedOrder]) .ToList<T>(); } /// <summary> /// 获得前几行数据 /// </summary> public List<T> FindAll(int top, SimpleExpression criteria, string filedOrder) { return Db() .FindAll(criteria) .OrderBy(Db()[filedOrder]) .Take(top) .ToList<List<T>>(); } /// <summary> /// 通过通过表达式查询查询 /// </summary> /// <param name="criteria">表达式</param> /// <returns>实体类对象</returns> public T Find(SimpleExpression criteria) { return Db().FindAll(criteria).FirstOrDefault(); } /// <summary> /// 分页 /// </summary> /// <param name="pageIndex">第几页</param> /// <param name="pageCount">每页总数</param> /// <param name="criteria">条件表达式</param> /// <param name="orderby"></param> /// <returns></returns> public List<T> FindAll(int pageIndex, int pageCount, SimpleExpression criteria, string orderby) { return Db().FindAll(criteria) .OrderBy(Db()[orderby]) .Skip((pageIndex - 1) * pageCount) .Take(pageCount) .ToList<T>(); } public List<T> FindAll(int pageIndex, int pageCount, SimpleExpression criteria, string orderby, out Promise<int> count) { return Db().FindAll(criteria) .OrderBy(Db()[orderby]) .WithTotalCount(out count) .Skip((pageIndex - 1) * pageCount) .Take(pageCount) .ToList<T>(); } #endregion /*//事务案例 * using (var transaction = database.BeginTransaction()) { transaction.Users.Insert(Name: "Steve", Age: 50); transaction.Commit(); } */ } }
调用:
SimpleManager<Blogs> simple = new SimpleManager<Blogs>();
var tt = simple.QueryAll();
foreach (var blogse in tt)
{
Console.WriteLine(blogse.Name + " , " + blogse.Url);
}