C# .NET ORM 框架 SqlSugar 5.X 版本

 

SqlSugar 5.X API

使用SqlSugar非常简单,而且功能强大。

SqlSugar=一个对象+一个参数=16个函数,

支持数据库:MySql、SqlServer、Sqlite、Oracle 、 postgresql

SqlSugar 16个功能

C# .NET ORM 框架 SqlSugar 5.X 版本_第1张图片

 

创建SqlSugarClient

所有操作都基于SqlSugarClient

SqlSugarClient参数,只有一个ConnectionConfig

public  List GetStudentList()
{
    var db= GetInstance();
    var list= db.Queryable().ToList();//Search
    return list;
}

/// 
/// Create SqlSugarClient
/// 
/// 
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; }
}

View more >>

1、查询方法

C# .NET ORM 框架 SqlSugar 5.X 版本_第2张图片

下面简单案例

//easy
var getAll = db.Queryable().ToList();
var getAllNoLock = db.Queryable().With(SqlWith.NoLock).ToList();
var getByPrimaryKey = db.Queryable().InSingle(2);
var sum = db.Queryable().Sum(it=>it.Id);
var isAny = db.Queryable().Where(it=>it.Id==-1).Any();
var isAny2 = db.Queryable().Any(it => it.Id == -1);
var getListByRename = db.Queryable().AS("Student").ToList();
var getByWhere = db.Queryable().Where(it => it.Id == 1 || it.Name == "a").ToList();
var getByFuns = db.Queryable().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList();
var group = db.Queryable().GroupBy(it => it.Id).Select(it =>new { id = SqlFunc.AggregateCount(it.Id) }).ToList();

//Page
var page = db.Queryable().ToPageList(pageIndex, pageSize, ref totalCount);

//page join
var pageJoin = db.Queryable((st, sc) =>new JoinQueryInfos(JoinType.Left,st.SchoolId==sc.Id))
.ToPageList(pageIndex, pageSize, ref totalCount);

//top 5
var top5 = db.Queryable().Take(5).ToList();

//join Order By (order by st.id desc,sc.id desc)
var list4 = db.Queryable((st, sc) =>new  JoinQueryInfos(JoinType.Left,st.SchoolId==sc.Id))
.OrderBy(st=>st.Id,OrderByType.Desc)
.OrderBy((st,sc)=>sc.Id,OrderByType.Desc)
.Select().ToList();

View more >>

2、更新方法

//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
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();

View more >>

3、新增方法

//Insert reutrn Insert Count
var t2 = db.Insertable(insertObj).ExecuteCommand();

//Insert reutrn Identity Value
var t3 = db.Insertable(insertObj).ExecuteReutrnIdentity();

//Only  insert  Name 
var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name,it.SchoolId }).ExecuteReutrnIdentity();

//Ignore TestId
var t5 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReutrnIdentity();

//Insert List
var s9 = db.Insertable(insertObjs).InsertColumns(it => new { it.Name }).ExecuteCommand();

View more >>

4、删除方法

//by entity
db.Deleteable().Where(new Student() { Id = 1 }).ExecuteCommand();

//by primary key
db.Deleteable().In(1).ExecuteCommand();

//by primary key array
db.Deleteable().In(new int[] { 1, 2 }).ExecuteCommand();

//by expression
db.Deleteable().Where(it => it.Id == 1).ExecuteCommand();

5、SQL 语句查询方法

var list = db.SqlQueryable("select * from student").ToPageList(1, 2);
var list2 = db.SqlQueryable("select * from student").Where(it=>it.Id==1).ToPageList(1, 2);
var list3= db.SqlQueryable("select * from student").Where("id=@id",new { id=1}).ToPageList(1, 2);

View more >>

5、保存顺序

var db = GetInstance();
db.Insertable(new Student() { Name = "a" }).AddQueue();
db.Insertable(new Student() { Name = "b" }).AddQueue();
db.SaveQueues(); 

db.Insertable(new Student() { Name = "a" }).AddQueue();
db.Insertable(new Student() { Name = "b" }).AddQueue();
db.Insertable(new Student() { Name = "c" }).AddQueue();
db.Insertable(new Student() { Name = "d" }).AddQueue();
var ar = db.SaveQueuesAsync(); 

db.Queryable().AddQueue();
db.Queryable().AddQueue();
db.AddQueue("select * from student where id=@id", new { id = 1 }); 
var result2 = db.SaveQueues();  

View more >>

Code generator

https://github.com/sunkaixuan/SoEasyPlatform

More APi 中文文档:

http://www.codeisbug.com/Doc/8

转载:https://gitee.com/sunkaixuan/SqlSugar

 

 

 

你可能感兴趣的:(架构设计)