使用SqlSugar ORM 操作国产数据库
Demo下载地址:https://github.com/sunkaixuan/SqlSugar
1、达梦数据库
更接近Oracle,更偏向自主研发,对开发人员友好度不如金仓
达梦公司在总结DM系列产品研发与应用经验的基础上,坚持开放创新、简洁实用的理念,历经五年匠心打磨,推出的新一代自研数据库。DM8吸收借鉴当前先...
下载地址:
http://www.dameng.com/down.aspx?TypeId=11&FId=t14:11:14
安装步骤:
选择Windows版本直接安装就行可以了,安装时需要授权跳过即可
2、人大金仓数据库
基本上和PgSql一模一样更贴近开发功能也继承了pgsql所有功能,对开发人员比较友好
Kingbase ES是面向事务处理类、兼顾分析类应用领域的新型数据库产品,致力于解决高并发、高可靠数据存储计算问题,是一款面向企事业单位管理信息系统、业务及生产系统、
决策支持系统等量身打造的承载数据库,目前功能性能已全面升级,是真正具有高成熟度的数据库产品。
下载地址:
https://www.kingbase.com.cn/index/service.html
安装步骤:
1.选择 金仓数据库管理系统
2、选择 安装文件包和授权文件包
创建项目
创建.NET Core项目
Nuget 只需要安装SqlSugarCore 就可以操作我们的国产数据库了
创建数据库对象
连接字符串格式
达梦 PORT=5236;DATABASE=DAMENG;HOST=localhost;PASSWORD=SYSDBA;USER ID=SYSDBA
人大金仓 Server=127.0.0.1;Port=54321;UID=SYSTEM;PWD=system;database=SQLSUGAR4XTEST1
具体代码如下:
//达梦
var Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.Dm,//达梦数据库
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute,
AopEvents = new AopEvents()
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
}
}
});
//人大金仓
var Db2 = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.Kdbndp,//人大金仓数据库
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute,
AopEvents = new AopEvents()
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
}
}
});
创建实体
[SugarTable("STudent")]
public class Student
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public int? SchoolId { get; set; }
public string Name { get; set; }
public DateTime? CreateTime { get; set; }
[SugarColumn(IsIgnore=true,NoSerialize =true)]
public int TestId { get; set; }
}
使用ORM操作数据库
具体用法和其它数据库操作一模一样
var getAll = db.Queryable<Student>().ToList();//查询所有
var getFirst = db.Queryable<Student>().First(it=>it.Id=1);//查询单条
var getAllNoLock = db.Queryable<Student>().With(SqlWith.NoLock).ToList();//SqlServer里面的withnolock
var getByPrimaryKey = db.Queryable<Student>().InSingle(2);//根据主键查询
var sum = db.Queryable<Student>().Sum(it=>it.Id);//查询总和
var isAny = db.Queryable<Student>().Where(it=>it.Id==-1).Any();//是否存在
var isAny2 = db.Queryable<Student>().Any(it => it.Id == -1);
var getListByRename = db.Queryable<School>().AS("Student").ToList();
var getByWhere = db.Queryable<Student>().Where(it => it.Id == 1 || it.Name == "a").ToList();
var list= db.Queryable<Student>().AS("student2019").ToList();//select * from student2019
var list2 = db.Queryable<Order>().Where(it =>it.Name.Contains("jack")).ToList();//模糊查询
返回List
1 |
|
返回第一个实体
Student item = db.Queryable<Student>().First(it=>it.Id==1);
返回DataTable
DataTable dataTable = db.Queryable<Student>().Select(it => it.Id).ToDataTable();
返回Json
var json = db.Queryable<Student>().ToJson();
返回数组
List<int> listInt=db.Queryable<Student>().Select(it => it.Id).ToList();
返回匿名对象
var dynamic = db.Queryable<Student>().Select<dynamic>().ToList();
返回到新类
var viewModel = db.Queryable<Student, School, DataTestInfo>((st, sc, di) => new JoinQueryInfos(
JoinType.Left, st.SchoolId == sc.Id,
JoinType.Left, st.Name == di.String
))
.Select<ViewModelStudent>().ToList();
多表返回匿名对象
var newDynamic = db.Queryable<Student, School, DataTestInfo>((st, sc, di) => new JoinQueryInfos(
JoinType.Left, st.SchoolId == sc.Id,
JoinType.Left, st.Name == di.String
)) .Select((st,sc,di)=>new { name=st.Name,scid=sc.Id }).ToList();
手动返回到新类
var newClass= db.Queryable<Student, School, DataTestInfo>((st, sc, di) => new JoinQueryInfos(
JoinType.Left, st.SchoolId == sc.Id,
JoinType.Left, st.Name == di.String
)) .Select((st,sc,di)=>new ClassName{ name=st.Name,scid=sc.Id }).ToList();
多表查一表
var oneClass = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
JoinType.Left, o.Id == i.OrderId,
JoinType.Left, o.CustomId == c.Id
))
.Select((o, i, c) => c).ToList();
多表返回2表
var twoClass = db.Queryable<Order, OrderItem, Custom>((o, i, c) => new JoinQueryInfos(
JoinType.Left, o.Id == i.OrderId,
JoinType.Left, o.CustomId == c.Id
))
.Select((o, i, c) => new { o,i}).ToList()
返回字典集合
List<Dictionary<string, object>> ListDic = db.Queryable<Student, School, DataTestInfo>((st, sc, di) => new JoinQueryInfos(
JoinType.Left, st.SchoolId == sc.Id,
JoinType.Left, st.Name == di.String
)).Select<ExpandoObject>().ToList().Select(it => it.ToDictionary(x => x.Key, x => x.Value)).ToList(); ;
删除
db.Deleteable<Student>().In(1).ExecuteCommand();
更新
var result= db.Updateable(updateObj).ExecuteCommand();
插入
db.Insertable(insertObj).ExecuteCommand();
源码 DEMO下载地址:
https://github.com/sunkaixuan/SqlSugar
谢谢支持
除了 增删查改,SqlSugar ORM 还支持很多功能,就不一一演示,一篇文章介绍不完。