using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Linq.Expressions; namespace Hanhe.iWCS.JingmenGEMNorthProtocol { public class SqlHelper where T : class, new() { public bool ExecuteSql(string sql, bool result = true) { try { var code = GetInstance(result).Ado.ExecuteCommand(sql); return code > 1; } catch (Exception ex) { return false; } } public bool Update(T model, string[] cols) { var db = GetInstance(); return db.Updateable(model).UpdateColumns(cols).ExecuteCommand() > 0; } public List GetList(Expression> where = null) { var db = GetInstance(); if (where == null) { return db.Queryable().ToList(); } else { return db.Queryable().Where(where).ToList(); } } public T Get(Expression> where, Expression> orderBy = null) { try { var db = GetInstance(); T model = null; if (orderBy != null) { model = db.Queryable().Where(where).OrderBy(orderBy, OrderByType.Desc).First();//查询表的所有 } else { model = db.Queryable().Where(where).First(); } return model; } catch (Exception ex) { Console.WriteLine($"读取SQLSERVER数据库异常!异常:{ex}"); return default(T); } } public bool Update(T model) { var db = GetInstance(); return db.Updateable(model).ExecuteCommand() > 0; } public bool Insert(T model) { try { var db = GetInstance(); return db.Insertable(model).ExecuteCommand() > 0; } catch (Exception ex) { Console.WriteLine("写入信息异常:" + ex.Message); return false; } } //删除指定条件数据 public bool Deleteable(Expression> where) { try { var db = GetInstance(); return db.Deleteable().Where(where).ExecuteCommand() > 0; } catch (Exception ex) { return false; } } //创建SqlSugarClient public SqlSugarClient GetInstance(bool action = true) { //创建数据库对象 if (action) { 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.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(); } public class SqlTwoHelper where T : class, new() { public bool ExecuteSql(string sql) { try { var code = GetInstance().Ado.ExecuteCommand(sql); return code > 1; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } } public bool Update(T model, string[] cols) { var db = GetInstance(); return db.Updateable(model).UpdateColumns(cols).ExecuteCommand() > 0; } public List GetList(Expression> where = null) { var 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 T Get(Expression> where, Expression> orderBy = null) { try { var db = GetInstance(); T model = null; if (orderBy != null) { model = db.Queryable().Where(where).OrderBy(orderBy, OrderByType.Desc).First();//查询表的所有 } else { model = db.Queryable().Where(where).First(); } return model; } catch (Exception ex) { Console.WriteLine(ex.Message); return default(T); } } public bool Update(T model) { var db = GetInstance(); return db.Updateable(model).ExecuteCommand() > 0; } public bool Insert(T model) { try { var db = GetInstance(); return db.Insertable(model).ExecuteCommand() > 0; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } } //删除指定条件数据 public bool Deleteable(Expression> where) { try { var db = GetInstance(); return db.Deleteable().Where(where).ExecuteCommand() > 0; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } } public bool CreateTable() { bool result = false; var db = GetInstance(); //定义SQL语句 //string sql = "create table student" + "(num varchar(50),name varchar(255),age int)"; try { // 创建数据库 //db.Context.DbMaintenance.CreateDatabase(); // 创建表 db.Context.CodeFirst.InitTables( typeof(T) ); //Console.WriteLine("数据表创建成功。"); } catch (Exception ae) { Console.WriteLine("数据表创建失败。" + ae.Message.ToString()); } finally { //db.Close();//对数据库操作完成后,需要关闭数据库,释放内存 } return result; } //创建SqlSugarClient public SqlSugarClient GetInstance() { //创建数据库对象 SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Settings.SqlServer, DbType = DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息 }); //db.MappingTables.Add("",""); //添加Sql打印事件,开发中可以删掉这个代码 //db.Aop.OnLogExecuting = (sql, pars) => { // Console.WriteLine(db.CurrentConnectionConfig.ConnectionString); // Console.WriteLine("sql"+sql+",pars"+ JsonConvert.SerializeObject(pars)); // //Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); // Console.WriteLine(); //}; return db; } } public class AMSSqlHelper { public bool ExecuteSql(string sql) { try { var code = GetInstance().Ado.ExecuteCommand(sql); return code > 1; } catch (Exception ex) { return false; } } private SqlSugarClient GetInstance() { //创建数据库对象 SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { //ConnectionString = "Server=192.168.1.233;Database=ams;Uid=root;Pwd=123456;",//连接符字串 ConnectionString = AMS_MSSQL,//连接符字串 DbType = DbType.SqlServer,//DbType.MySql IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息 }); //添加Sql打印事件,开发中可以删掉这个代码 //db.Aop.OnLogExecuting = (sql, pars) => { // Console.WriteLine(db.CurrentConnectionConfig.ConnectionString); // Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); // Console.WriteLine(); //}; return db; } //实体与数据库结构一样 public static string AMS_MSSQL = ConfigurationManager.ConnectionStrings["AMS_MSSQL"].ToString(); } }