.net SqlSugarHelper

NuGet安装: SqlSugarCore

cpp 复制代码
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Namespace 
{
    public class SqlSugarHelper
    {
        public string _connectionString = CustomConfigManager.GetConfig("MySql");//MySql连接字符串
        public SqlSugarClient _db = null;

        /// <summary>
        /// 构造函数(初始化)
        /// 调用方法:
        /// SqlSugarHelper sugar = new SqlSugarHelper();
        /// var db = sugar.SqlClient();
        /// var user = db.Queryable<Userinfo>().Where(a => a.Name.Equals(uid) && a.PWD.Equals(pwd)).ToList().FirstOrDefault();
        /// </summary>
        public SqlSugarClient SqlClient()
        {
            if (string.IsNullOrEmpty(_connectionString))
                throw new ArgumentNullException("数据库连接字符串为空");

            _db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = _connectionString,//数据库连接字符串,见UI的appsettings.json内配置
                DbType = DbType.MySql,//数据库类型
                IsAutoCloseConnection = true,//自动断开连接
                MoreSettings = new ConnMoreSettings()
                {
                    IsWithNoLockQuery = true,//为true表式查询的时候默认会加上.With(SqlWith.NoLock),
                    IsAutoRemoveDataCache = true//为true自动清除缓存
                }
            });
            //输入最终SQL语句...
            _db.Aop.OnLogExecuting = (sql, pars) =>
            {
                var s1 = sql;//断点打在这里看内部生成的sql语句...
            };

            return _db;
        }

    }
}

config文件:

cs 复制代码
{
  //MySql连接字符串
  "MySql": "Server=127.0.0.1;Database=db_test;User ID=root;Password=123456;SslMode=None;allowPublicKeyRetrieval=true;"//开发环境
}

调用举例:

cs 复制代码
    //查询
    public static List<tb_equip> Get_EquipList()
    {
            SqlSugarHelper sugar = new SqlSugarHelper();
            var db = sugar.SqlClient();
            try
            {
                var gsList = db.Queryable<tb_equip>()
                   .Where(a => a.IsUsing == true)//使用中
                   .WhereIF(!string.IsNullOrWhiteSpace(PowerStation), a => a.PowerStation.Equals(PowerStation))//可选条件
                   .ToList();

                return gsList;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Get_TH70M_EquipList() " + ex.Message);//打印结果
                return null;
            }
    }


    //添加
    public static bool tb_tcp_log_add(Socket send, decimal wd_dec, decimal sd_dec, tb_equip equip, string packet, TcpType tcpType)
    {
        SqlSugarHelper sugar = new SqlSugarHelper();
        var db = sugar.SqlClient();
        tb_tcp_log log = new tb_tcp_log();
        log.TCP_IP_STR = send.RemoteEndPoint.ToString();//IP原文
        log.TCP_IP = Tools.SocketFormat(send.RemoteEndPoint.ToString());//IP格式化
        log.UUID = Guid.NewGuid().ToString();//主键(必须)
        log.Type = tcpType.ToString(); //类型

        int cc = db.Insertable(log).ExecuteCommand();//添加到数据库
        Console.WriteLine($"添加到数据库,成功添加{cc}条...");//打印结果
        if (cc > 0)
        {
            WriteLineAndLog("已添加");
        }

        return cc > 0;
    }
相关推荐
逻极18 小时前
C# 从入门到精通:现代云原生与AI应用开发实战
ai·云原生·c#·.net
geovindu1 天前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
海盗12342 天前
微软技术周报2026-07-27
microsoft·c#·.net
iReachers2 天前
.NET项目集成混淆加密:自动化保护DLL和EXE
.net·代码保护·自动构建·msbuild·c#混淆
完美火龙篇 四月的友3 天前
通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
.net·.netcore
柠檬香的3 天前
CsGrafeq: 比 Desmos 更“能折腾”的几何函数画板(.NET + Avalonia)
.net
geovindu4 天前
CSharp: Iterative Algorithms
开发语言·后端·算法·c#·.net·迭代算法
geovindu6 天前
CSharp: Flyweight Pattern
开发语言·后端·c#·.net·享元模式·结构型模式
驰骋工作流6 天前
请假流程:六款.NET工作流引擎实现方式对比
.net·ccflow·工作流引擎二开·.net工作流引擎对比