SqlSugar, .Net优秀的ORM

博客 分享
0 1125
张三
张三 2021-06-20 18:10:30
悬赏:10 积分 收藏

SqlSugar, .Net优秀的ORM

SqlSugar是什么

SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法。

SqlSugar优点

1、高性能 ,不夸张的说,去掉Sql在数据库执行的时间,SqlSugar是EF数倍性能,另外在批量操作和一对多查询上也有不错的SQL优化

2、高扩展性 ,支持自定义拉姆达函数解析、扩展数据类型、支持自定义实体特性,外部缓存等

3、稳定性和技术支持,  虽然不是官方ORM, 但在稳定性上也是有着数年用户积累,如果遇到问题可以在GITHUB提出来,会根据紧急度定期解决

4、功能全面,虽然SqlSugar小巧可功能并不逊色于EF框架

5、创新、持续更新 ,向下兼容

SqlSugar的功能

SqlSugar查询特色

SqlSugar实体类使用工具

Github地址:https://github.com/q7164518/SqlSugar.Tools

SqlSugar在项目中使用

Nuget安装

命令安装

Install-Package sqlSugar

Install-Package sqlSugarCore

SqlSugar开整

建SqlSugar帮助类

  public class SqlSugarHelper

    {

        private static string _Connection = "server=.;uid=sa;pwd=18338149719;database=Student";

 

        public static SqlSugarClient DB

        {

            get => new SqlSugarClient(new ConnectionConfig()

            {

                ConnectionString = _Connection,//必填, 数据库连接字符串

                DbType = DbType.SqlServer,         //必填, 数据库类型

                IsAutoCloseConnection = true,       //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作

                InitKeyType = InitKeyType.SystemTable    //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息

            });

        }

}

SqlSugart增加操作

         Student stu = new Student();

                stu.UserName = "123456";

                stu.Name = "张三";

 

SqlSugar插入操作

 

                Console.WriteLine("使用SqlSugar进行插入操作,返回受影响的行数");

                int iResult = SqlSugarHelper.DB.Insertable(stu).ExecuteCommand();

 

 

                Console.WriteLine("插入并返回自增列用ExecuteReutrnIdentity");

                int iResult1 = SqlSugarHelper.DB.Insertable(stu).ExecuteReturnIdentity();

                long iResult2 = SqlSugarHelper.DB.Insertable(stu).ExecuteReturnBigIdentity(); //4.5.0.2 +  long

 

                Console.WriteLine("插入并返回实体 ,只是自identity 添加到 参数的实体里面并返回,没有查2次库,所以有些默认值什么的变动是取不到的你们需要手动进行2次查询获取");

                Student stu1 = SqlSugarHelper.DB.Insertable(stu).ExecuteReturnEntity();

 

 

                Console.WriteLine("插入并返回bool, 并将插入结果赋值到实体");

                bool bReult = SqlSugarHelper.DB.Insertable(stu).ExecuteCommandIdentityIntoEntity();

 

SqlSugar批量插入

                List<Student> students = new List<Student>();

                Student stu2 = new Student();

                stu2.UserName = "123456";

                stu2.Name = "李四";

                students.Add(stu2);

                students.Add(stu1);

                students.Add(stu);

                int iResultList = SqlSugarHelper.DB.Insertable(students.ToArray()).ExecuteCommand();

 

SqlSugar删除

                //删除操作

                //   根据实体删除(实体内主键一定要有值)

 

                int t0 = SqlSugarHelper.DB.Deleteable<Student>().Where(new Student() { Id = 1 }).ExecuteCommand();

 

 

                //  根据实体集删除

 

                int t1 = SqlSugarHelper.DB.Deleteable<Student>().Where(new List<Student>() { new Student() { Id = 2 } }).ExecuteCommand();

 

 

                // 根据主键删除

 

                int t3 = SqlSugarHelper.DB.Deleteable<Student>().In(3).ExecuteCommand();

 

 

                //  根据主键批量删除

 

                int t4 = SqlSugarHelper.DB.Deleteable<Student>().In(new int[] { 4, 5 }).ExecuteCommand();

 

SqlSugar更新操作

                //更新操作

                //根据实体更新(主键要有值,主键是更新条件)

                stu.Name = "王五";

                var t13 = SqlSugarHelper.DB.Updateable(stu).ExecuteCommand(); //这种方式会以主键为条件

 

SqlSugar查询操作

                // 查询操作

                //查询所有

                var getAll = SqlSugarHelper.DB.Queryable<Student>().ToList();

 

                //  IN查询

 

                //Id In (7,8,9), 指定列In查询

                var in1 = SqlSugarHelper.DB.Queryable<Student>().In(it => it.Id, new int[] { 7, 8, 9 }).ToList();

 

 

                // 条件查询

 

                var getByWhere = SqlSugarHelper.DB.Queryable<Student>().Where(it => it.Id != 1).ToList();

 

                // 分页查询

                var pageIndex = 1;

                var pageSize = 10;

                var totalCount = 0;

                var page = SqlSugarHelper.DB.Queryable<Student>().OrderBy(it => it.Id).ToPageList(pageIndex, pageSize, ref totalCount);

总结

 轻便,快捷,简单易用,容易上手。

 下篇想看什么可在下方留言。

回帖
    张三

    张三 (王者 段位)

    1111 积分 (2)粉丝 (41)源码

     

    温馨提示

    亦奇源码

    最新会员