SqlSugar学习总结1(基础操作)

官方网站:https://www.donet5.com/Home/Doc

SqlSugar属于orm框架,但比EF更加轻量级,性能也更优越。

下面用示例演示相关用法

项目结构:

 

项目需要应用程序集:SqlSugarCore

BaseOperate类 

using SqlSugar;
using SqlSugarStart.DbModels;
using System;
using System.Collections.Generic;

namespace SqlSugarStart
{
    public static class BaseOperate
    {
        public static void test1()
        {
            try
            {
                SqlSugarClient sqlSugarClient = new SqlSugarClient(new ConnectionConfig
                {
                    DbType = DbType.SqlServer,//要连接的数据库类型
                    ConnectionString = "server=.;uid=sa;pwd=123456;database=SqlSugarTest",//sqlsqver数据库链接字符串
                    IsAutoCloseConnection = true
                });

                #region 创建数据库和表的语句仅执行一次
                sqlSugarClient.DbMaintenance.CreateDatabase(); //创建数据库
                sqlSugarClient.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(Commodity));//创建表
                #endregion

                sqlSugarClient.Aop.OnLogExecuted = (sql, pra) =>
                {
                    Console.WriteLine("*****************************************************");
                    Console.WriteLine($"sql语句:{sql}");
                };


                //新增一条记录
                sqlSugarClient.Insertable<Commodity>(new Commodity()
                {
                    ProductId = 1,
                    CategoryId = 1,
                    Title = "测试",
                    Price = 1,
                    Url = "test",
                    ImageUrl = "testtest"
                }).ExecuteCommand();

                List<Commodity> list = sqlSugarClient.Queryable<Commodity>().ToList();//查询多条数据

                Commodity commodity = sqlSugarClient.Queryable<Commodity>().First();//查询单条数据

                commodity.ImageUrl = commodity.ImageUrl + "11111";
                sqlSugarClient.Updateable<Commodity>(commodity).ExecuteCommand();//修改

                sqlSugarClient.Deleteable<Commodity>(commodity).ExecuteCommand();//删除

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

Commodity类

using SqlSugar;

namespace SqlSugarStart.DbModels
{
    [SugarTable("Commodity")]
    public class Commodity
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        public long ProductId { get; set; }
        public int CategoryId { get; set; }
        public string Title { get; set; }
        public decimal Price { get; set; }
        public string Url { get; set; }
        public string ImageUrl { get; set; }
    }
}

Main方法

using System;

namespace SqlSugarStart
{
    class Program
    {
        /// <summary>
        /// sqlsugar相关
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            BaseOperate.test1();

            Console.ReadLine();
        }
    }
}

数据库和数据库中的表都不需要提前创建,SqlSugar可以帮助我们创建数据库和表

执行上面这两代码SqlSugar就会自动创建数据库和表。这段代码一般只执行一次,创建完成就注释,避免重复执行,影响程序执行效率。

执行结果:

在修改语句加上断点,可以去数据库查询到新增的记录

 数据库查询结果

下面是程序执行过程中对应的sql语句

 

 SqlSugar底层还是执行的SQL语句。

Guess you like

Origin blog.csdn.net/liangmengbk/article/details/121875314