使用介绍
1.配置文件配置好连接串
connectionName的值对应连接串的name
<appSettings> ???<add key="connectionName" value="DB"/> ?</appSettings> ?<connectionStrings> ???<!--<add name="DB" connectionString="Data Source=localhost/XE;User Id=hr;Password=hr;" providerName="Oracle.ManagedDataAccess" />--> ???<!--<add name="DB" connectionString="host=localhost;database=test;uid=root;pwd=sa123456;charset=utf8;" providerName="MySql.Data.MySqlClient" />--> ???<add name="DB" connectionString="Data Source=localhost;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=123456;" providerName="System.Data.SqlClient" /></connectionStrings>
2.定义好实体
public class Book ???{ ???????[Key] ???????[DatabaseGenerated(DatabaseGeneratedOption.Identity)] ???????public int? BookID { get; set; } ???????public string BookName { get; set; } ???????public int? AuthorID { get; set; } ???????[NotMapped] ???????public string AuthorName { get; set; } ???????public double? Price { get; set; } ???????public DateTime? PublishDate { get; set; } ???}
增删改查 目前支持sqlserver、mysql、oracle
增加
???????????using (DNetContext db = new DNetContext()) ???????????{ ???????????????var authorid = db.Add(new Author { AuthorName = "张三", Age = 30, IsValid = true }); ???????????????db.Add(new Book { BookName = "从入门到放弃", Price = 20.5, PublishDate = DateTime.Now, AuthorID = authorid }); ???????????}
修改
???????????using (DNetContext db = new DNetContext()) ???????????{
???????????????int authorid = db.GetMax<Author>(m => (int)m.AuthorID); ???????????????db.Update<Author>(m => m.AuthorName = "恶魔猎手", m => m.AuthorID == authorid); ???????????}
删除
???????????using (DNetContext db = new DNetContext()) ???????????{ ???????????????var author = db.GetSingle<Author>(m => true, q => q.OrderBy(m => m.AuthorID)); ???????????????var effect = db.Delete(author); ???????????????int authorid = db.GetMax<Author>(m => (int)m.AuthorID); ???????????????db.Delete<Author>(m => m.AuthorID == authorid); ???????????}
查询
单表查询
支持以下sql格式
like
Contains、StartsWith、EndsWith
in
Contains 集合类
upper lower
ToUpper、ToLower
sql字符大小比较
CompareTo、Equals
charindex instr
IndexOf
is null or = ‘‘
IsNullOrEmpty
类型转化、时间格式化
ToString
整型转化
ToInt32
trim
TrimStart、TrimEnd、Trim
???????????using (DNetContext db = new DNetContext()) ???????????{ ???????????????var author = db.GetSingle<Author>(m => true, q => q.OrderBy(m => m.AuthorID)); ???????????????author = db.GetSingle<Author>(m => m.AuthorName.Contains("李四") && m.IsValid == true); ???????????????var authors= db.GetList<Author>(m => m.AuthorName.StartsWith("张三") && m.IsValid == true); ???????????????//获取动态类型 ???????????????List<dynamic> name = db.GetDistinctList<Author>(m => m.AuthorName.StartsWith("王五") && m.IsValid == true,m=>m.AuthorName); ???????????????????????????????List<string> name1 = db.GetDistinctList<Author,string>(m => m.AuthorName.StartsWith("王五") && m.IsValid == true, m => m.AuthorName); ???????????????????????????????//获取最大值 ???????????????var authorid = db.GetMax<Author>(m => (int)m.AuthorID); ???????????????//动态查询 ???????????????WhereBuilder<Author> where = new WhereBuilder<Author>(); ???????????????where.And(m=>m.AuthorName.Contains("张三")); ???????????????where.And(m => m.AuthorID==3); ???????????????where.Or(m=>m.IsValid==true); ???????????????db.GetList<Author>(where.WhereExpression); ???????????????//分页参数由前台传来 ???????????????PageFilter page = new PageFilter { PageIndex=1, PageSize=10 }; ???????????????page.And<Author>(m=> "守望者的天空".Contains(m.AuthorName)); ???????????????page.OrderBy<Author>(q=>q.OrderBy(m=>m.AuthorName).OrderByDescending(m=>m.AuthorID)); ???????????????PageDataSource<Author> pageSource= db.GetPage<Author>(page); ???????????}
多表查询
???????????using (DNetContext db = new DNetContext()) ???????????{ ???????????????var books = db.JoinQuery.LeftJoin<Book, Author>((m, n) => m.AuthorID == n.AuthorID && n.IsValid == true) ????????????????????.Fields<Book, Author>((m, n) => new { m, n.AuthorName }) ????????????????????.OrderByAsc<Book>(m => m.BookName) ????????????????????.Select<Book>(); ???????????????var join = db.JoinQuery.LeftJoin<Book, Author>((m, n) => m.AuthorID == n.AuthorID && n.IsValid == true) ????????????????????.Fields<Book, Author>((m, n) => new { m, n.AuthorName }) ????????????????????.OrderByAsc<Book>(m => m.BookName); ???????????????PageFilter page = new PageFilter { PageIndex = 1, PageSize = 10 };//分页参数前台传来 ???????????????join.SelectPage<Book>(page); ???????????}
事务的支持
???????????using (DNetContext db = new DNetContext()) ???????????{ ???????????????db.DataBase.BeginTransaction(); ???????????????try ???????????????{ ???????????????????List<Author> authors = new List<Author>(); ???????????????????for (int i = 0; i <= 100; i++) ???????????????????{ ???????????????????????authors.Add(new Author { AuthorName = "测试" + i.ToString(), Age = 20, IsValid = true }); ???????????????????} ???????????????????db.Add(authors); ???????????????????db.DataBase.Commit(); ???????????????} ???????????????catch ???????????????{ ???????????????????db.DataBase.Rollback(); ???????????????} ???????????}
SQL语句查询的支持
???????????using (DNetContext db = new DNetContext()) ???????????{ ???????????????StringBuilder sql = new StringBuilder(); ???????????????List<DbParameter> parameters = new List<DbParameter>(); ???????????????sql.AppendFormat(@"SELECT {0},A.AuthorName FROM Book B LEFT JOIN Author A ON A.AuthorID=B.AuthorID WHERE", SqlBuilder.GetSelectAllFields<Book>("B")); ???????????????sql.Append(" B.BookID>@BookID "); ???????????????parameters.Add(db.GetDbParameter("BookID",1)); ???????????????PageDataSource<Book> books = db.GetPage<Book>(sql.ToString(),new PageFilter { PageIndex=1, PageSize=5 }, parameters.ToArray()); ???????????????List<Book> bks = db.GetList<Book>(sql.ToString(), parameters.ToArray()); ???????????}
欢迎技术讨论
最新代码下载
https://files.cnblogs.com/files/DNetORM/DNet.ORM4.0.rar
DNetORM 一款轻量级的ORM框架
原文地址:http://www.cnblogs.com/DNetORM/p/8000373.html