using Hanhe.iWCS.Common; using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace Hanhe.iWCS.JingmenGEMTwoProtocol { public class SqlHelper where T : class, new() { public bool ExecuteSql(string sql, bool result = true) { try { var code = GetInstance(result,2).Ado.ExecuteCommand(sql); return code > 0; } catch (Exception ex) { CMMLog.Info($"更新数据库失败,错误原因可能是:{ex.Message}"); return false; } } public List GetList(Expression> where = null) { SqlSugarClient db = GetInstance(); if (where == null) { return db.Queryable().ToList(); } else { return db.Queryable().Where(where).ToList(); } } public T Get(Expression> where, Expression> order, bool asc = false) { SqlSugarClient db = GetInstance(); if (order == null) { return db.Queryable().Where(where).Single(); } else { return db.Queryable().Where(where).OrderBy(order, asc ? OrderByType.Asc : OrderByType.Desc).First(); } } public bool Update(T model) { SqlSugarClient db = GetInstance(); return db.Updateable(model).ExecuteCommand() > 0; } //删除指定条件数据 public bool Deleteable(Expression> where) { SqlSugarClient db = GetInstance(); return db.Deleteable().Where(where).ExecuteCommand() > 0; } //创建SqlSugarClient public SqlSugarClient GetInstance(bool action = true, int flage = 1) { //创建数据库对象 if (action) { if(flage == 1) { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { //ConnectionString = "Server=192.168.1.233;Database=ams;Uid=root;Pwd=123456;",//连接符字串 ConnectionString = Settings.SqlServer,//连接符字串 DbType = DbType.SqlServer,//DbType.MySql IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息 }); //添加Sql打印事件,开发中可以删掉这个代码 db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); }; return db; } else { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { //ConnectionString = "Server=192.168.1.233;Database=ams;Uid=root;Pwd=123456;",//连接符字串 ConnectionString = Settings.SqlServer2,//连接符字串 DbType = DbType.SqlServer,//DbType.MySql IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息 }); //添加Sql打印事件,开发中可以删掉这个代码 db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); }; return db; } } else { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { //ConnectionString = "Server=192.168.1.233;Database=ams;Uid=root;Pwd=123456;",//连接符字串 ConnectionString = Settings.SqlServer1,//连接符字串 DbType = DbType.SqlServer,//DbType.MySql IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息 }); //添加Sql打印事件,开发中可以删掉这个代码 db.Aop.OnLogExecuting = (sql, pars) => { //Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); }; return db; } } public static string AMS_MSSQL = ConfigurationManager.ConnectionStrings["AMS_MSSQL"].ToString(); } }