.net SqlSugarHelper

打印 上一主题 下一主题

主题 958|帖子 958|积分 2874

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
NuGet安装: SqlSugarCore
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Namespace
  8. {
  9.     public class SqlSugarHelper
  10.     {
  11.         public string _connectionString = CustomConfigManager.GetConfig("MySql");//MySql连接字符串
  12.         public SqlSugarClient _db = null;
  13.         /// <summary>
  14.         /// 构造函数(初始化)
  15.         /// 调用方法:
  16.         /// SqlSugarHelper sugar = new SqlSugarHelper();
  17.         /// var db = sugar.SqlClient();
  18.         /// var user = db.Queryable<Userinfo>().Where(a => a.Name.Equals(uid) && a.PWD.Equals(pwd)).ToList().FirstOrDefault();
  19.         /// </summary>
  20.         public SqlSugarClient SqlClient()
  21.         {
  22.             if (string.IsNullOrEmpty(_connectionString))
  23.                 throw new ArgumentNullException("数据库连接字符串为空");
  24.             _db = new SqlSugarClient(new ConnectionConfig()
  25.             {
  26.                 ConnectionString = _connectionString,//数据库连接字符串,见UI的appsettings.json内配置
  27.                 DbType = DbType.MySql,//数据库类型
  28.                 IsAutoCloseConnection = true,//自动断开连接
  29.                 MoreSettings = new ConnMoreSettings()
  30.                 {
  31.                     IsWithNoLockQuery = true,//为true表式查询的时候默认会加上.With(SqlWith.NoLock),
  32.                     IsAutoRemoveDataCache = true//为true自动清除缓存
  33.                 }
  34.             });
  35.             //输入最终SQL语句...
  36.             _db.Aop.OnLogExecuting = (sql, pars) =>
  37.             {
  38.                 var s1 = sql;//断点打在这里看内部生成的sql语句...
  39.             };
  40.             return _db;
  41.         }
  42.     }
  43. }
复制代码

config文件:
  1. {
  2.   //MySql连接字符串
  3.   "MySql": "Server=127.0.0.1;Database=db_test;User ID=root;Password=123456;SslMode=None;allowPublicKeyRetrieval=true;"//开发环境
  4. }
复制代码

调用举例:
  1.     //查询
  2.     public static List<tb_equip> Get_EquipList()
  3.     {
  4.             SqlSugarHelper sugar = new SqlSugarHelper();
  5.             var db = sugar.SqlClient();
  6.             try
  7.             {
  8.                 var gsList = db.Queryable<tb_equip>()
  9.                    .Where(a => a.IsUsing == true)//使用中
  10.                    .WhereIF(!string.IsNullOrWhiteSpace(PowerStation), a => a.PowerStation.Equals(PowerStation))//可选条件
  11.                    .ToList();
  12.                 return gsList;
  13.             }
  14.             catch (Exception ex)
  15.             {
  16.                 Console.WriteLine("Get_TH70M_EquipList() " + ex.Message);//打印结果
  17.                 return null;
  18.             }
  19.     }
  20.     //添加
  21.     public static bool tb_tcp_log_add(Socket send, decimal wd_dec, decimal sd_dec, tb_equip equip, string packet, TcpType tcpType)
  22.     {
  23.         SqlSugarHelper sugar = new SqlSugarHelper();
  24.         var db = sugar.SqlClient();
  25.         tb_tcp_log log = new tb_tcp_log();
  26.         log.TCP_IP_STR = send.RemoteEndPoint.ToString();//IP原文
  27.         log.TCP_IP = Tools.SocketFormat(send.RemoteEndPoint.ToString());//IP格式化
  28.         log.UUID = Guid.NewGuid().ToString();//主键(必须)
  29.         log.Type = tcpType.ToString(); //类型
  30.         int cc = db.Insertable(log).ExecuteCommand();//添加到数据库
  31.         Console.WriteLine($"添加到数据库,成功添加{cc}条...");//打印结果
  32.         if (cc > 0)
  33.         {
  34.             WriteLineAndLog("已添加");
  35.         }
  36.         return cc > 0;
  37.     }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

八卦阵

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表