细胞de理想

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  106 随笔 :: 1 文章 :: 4 评论 :: 11万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

官方网站: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语句。

 

posted on   细胞的理想  阅读(322)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示