|
|
|
|
|
|
|
## 👨🎓果糖大数据科技(南通)有限公司(永久免费开源) ##
|
|
|
|
公司地址:南通太阳晶城大厦3幢125室
|
|
|
|
|
|
|
|
| qq | 微信 | Email | QQ Group |
|
|
|
|
| ---------| ---------| ---------| ---------|
|
|
|
|
| 610262374 | sabimao | 610262374@qq.com | 225982985 |
|
|
|
|
|
|
|
|
服务项目:接收项目外包等服务(高级程序员 & 需求分析 & 软件架构师),技术咨询,技术顾问
|
|
|
|
|
|
|
|
## 👮♀️中文文档:
|
|
|
|
|
|
|
|
世界上最简单的ORM,只需要配置连接字符串,F5运行控制台自动建库建表运行DEMO
|
|
|
|
|
|
|
|
<font color=#0099ff size=72> 地址:</font> http://donet5.com/Home/Doc
|
|
|
|
|
|
|
|
|
|
|
|
# 🕵️♀️English documents
|
|
|
|
|
|
|
|
Using SqlSugar is very simple , And it's powerful.
|
|
|
|
|
|
|
|
SqlSugar=One object+One parameter=16 functions,
|
|
|
|
|
|
|
|
Support:MySql、SqlServer、Sqlite、Oracle 、 postgresql 、达梦、人大金仓
|
|
|
|
|
|
|
|
|
|
|
|
## 📮Nuget
|
|
|
|
|
|
|
|
|.net |.net core |
|
|
|
|
|---------| ---------|
|
|
|
|
|Install-Package sqlSugar |Install-Package sqlSugarCore|
|
|
|
|
|
|
|
|
## 📑SqlSugar's 16 Functions
|
|
|
|
There are 16 methods under SqlSugarClient
|
|
|
|
![输入图片说明](http://www.donet5.com/_theme/ueditor/utf8-net/net/upload/image/20190430/6369224056499802674782957.jpg?id=111 "sqlsugar")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 🎀 Create SqlSugarClient
|
|
|
|
All operations are based on SqlSugarClient
|
|
|
|
|
|
|
|
SqlSugarClient parameter and only one ConnectionConfig
|
|
|
|
```cs
|
|
|
|
public List<Student> GetStudentList()
|
|
|
|
{
|
|
|
|
var db = GetInstance();
|
|
|
|
var list = db.Queryable<Student>().ToList();//Search
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create SqlSugarClient
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
private SqlSugarClient GetInstance()
|
|
|
|
{
|
|
|
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
|
|
|
{
|
|
|
|
ConnectionString = "Server=.xxxxx",
|
|
|
|
DbType = DbType.SqlServer,
|
|
|
|
IsAutoCloseConnection = true,
|
|
|
|
InitKeyType = InitKeyType.Attribute
|
|
|
|
});
|
|
|
|
//Print sql
|
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
{
|
|
|
|
Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
|
|
Console.WriteLine();
|
|
|
|
};
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Student
|
|
|
|
{
|
|
|
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
|
|
|
public int Id { get; set; }
|
|
|
|
public int? SchoolId { get; set; }
|
|
|
|
public string Name { get; set; }
|
|
|
|
}
|
|
|
|
```
|
|
|
|
[<font color=red>View more >> </font>](https://github.com/sunkaixuan/SqlSugar/wiki/0.SqlSugarClient)
|
|
|
|
|
|
|
|
|
|
|
|
## 🎉 1. Queryable
|
|
|
|
We use it to query
|
|
|
|
![输入图片说明](http://www.donet5.com/_theme/ueditor/utf8-net/net/upload/image/20190502/6369240932997363035197459.png?id=1112 "Queryable")
|
|
|
|
|
|
|
|
Here are some examples
|
|
|
|
```cs
|
|
|
|
//easy
|
|
|
|
var getAll = db.Queryable<Student>().ToList();
|
|
|
|
var getAllNoLock = db.Queryable<Student>().With(SqlWith.NoLock).ToList();
|
|
|
|
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 getByFuns = db.Queryable<Student>().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList();
|
|
|
|
var group = db.Queryable<Student>().GroupBy(it => it.Id).Select(it => new { id = SqlFunc.AggregateCount(it.Id) }).ToList();
|
|
|
|
|
|
|
|
//Page
|
|
|
|
var page = db.Queryable<Student>().ToPageList(pageIndex, pageSize, ref totalCount);
|
|
|
|
|
|
|
|
//page join
|
|
|
|
var pageJoin = db.Queryable<Student, School>((st, sc) => new JoinQueryInfos(JoinType.Left, st.SchoolId == sc.Id))
|
|
|
|
.ToPageList(pageIndex, pageSize, ref totalCount);
|
|
|
|
|
|
|
|
//top 5
|
|
|
|
var top5 = db.Queryable<Student>().Take(5).ToList();
|
|
|
|
|
|
|
|
//join Order By (order by st.id desc,sc.id desc)
|
|
|
|
var list4 = db.Queryable<Student, School>((st, sc) => new JoinQueryInfos(JoinType.Left, st.SchoolId == sc.Id))
|
|
|
|
.OrderBy(st => st.Id, OrderByType.Desc)
|
|
|
|
.OrderBy((st, sc) => sc.Id, OrderByType.Desc)
|
|
|
|
.Select<ViewModelStudent>().ToList();
|
|
|
|
|
|
|
|
```
|
|
|
|
[<font color=red>View more >> </font>](https://github.com/sunkaixuan/SqlSugar/wiki/1.Queryable)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 🎊 2. Updateable
|
|
|
|
We use it to Update
|
|
|
|
```cs
|
|
|
|
//update reutrn Update Count
|
|
|
|
var t1= db.Updateable(updateObj).ExecuteCommand();
|
|
|
|
|
|
|
|
//Only update Name
|
|
|
|
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
|
|
|
|
|
|
|
|
//Ignore Name and TestId
|
|
|
|
var t4 = db.Updateable(updateObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteCommand();
|
|
|
|
|
|
|
|
//update List<T>
|
|
|
|
var t7 = db.Updateable(updateObjs).ExecuteCommand();
|
|
|
|
|
|
|
|
//Where By Expression
|
|
|
|
var t9 = db.Updateable(it => new class() { name = "a",createtime = p }).Where(it => it.Id == 1).ExecuteCommand();
|
|
|
|
|
|
|
|
```
|
|
|
|
[<font color=red>View more >> </font>](https://github.com/sunkaixuan/SqlSugar/wiki/2.Updateable)
|
|
|
|
|
|
|
|
|
|
|
|
## 🎃 3. Insertable
|
|
|
|
We use it to Insert
|
|
|
|
```cs
|
|
|
|
//Insert reutrn Insert Count
|
|
|
|
var t2 = db.Insertable(insertObj).ExecuteCommand();
|
|
|
|
|
|
|
|
//Insert reutrn Identity Value
|
|