using NLog; using SqlSugar; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Diagnostics; using System.Linq; namespace DeviceRepair.DataAccess { public class DbContext where T : class, new() { private static readonly string connectionString = DeviceRepair.Utils.Config.Configurations.Properties.Conn; private static readonly string logConn = DeviceRepair.Utils.Config.Configurations.Properties.logConn; private static readonly string sfc = DeviceRepair.Utils.Config.Configurations.Properties.SfcConn; public SqlSugarClient db; //{ // get // { // var _db = new SqlSugarClient(new List { // new ConnectionConfig() // { // ConnectionString = connectionString, // DbType = DbType.SqlServer, // InitKeyType = InitKeyType.Attribute, // IsAutoCloseConnection = true, // ConfigId = "main" // }, // new ConnectionConfig() // { // ConnectionString = logConn, // DbType = DbType.SqlServer, // InitKeyType = InitKeyType.Attribute, // IsAutoCloseConnection = true, // ConfigId = "log" // } // }); // _db.Aop.OnLogExecuting = (sql, pars) => // { // Debug.WriteLine(sql + "\r\n" + // db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); // }; // return _db; // } //} public SimpleClient CurrentDb { get { return new SimpleClient(db); } } public DbContext() { SqlConnectionStringBuilder TsSFCdb = new SqlConnectionStringBuilder(sfc); TsSFCdb.InitialCatalog = "SFCData"; string SFCData = TsSFCdb.ConnectionString; TsSFCdb.InitialCatalog = "SFCAddOn"; string SFCAddOn = TsSFCdb.ConnectionString; db = new SqlSugarClient(new List { new ConnectionConfig() { ConnectionString = connectionString, DbType = DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConfigId = "main" }, new ConnectionConfig() { ConnectionString = logConn, DbType = DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConfigId = "log" }, new ConnectionConfig() { ConnectionString = SFCData, DbType = DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConfigId = "data" }, new ConnectionConfig() { ConnectionString = SFCAddOn, DbType = DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConfigId = "addon" } }); //调式代码 用来打印SQL db.Aop.OnLogExecuting = (sql, pars) => { Debug.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); }; } /// /// 获取所有 /// /// public virtual List GetList() { try { return CurrentDb.GetList(); } catch (SqlSugarException) { throw; } catch (Exception) { throw; } } /// /// 根据主键获取单条数据 /// /// /// public virtual T GetSingle(int Pk) { try { if (Pk <= 0) return null; return CurrentDb.GetById(Pk); } catch (SqlSugarException) { throw; } catch (Exception) { throw; } } /// /// 根据主键删除 /// /// /// public virtual bool Delete(dynamic id) { try { return CurrentDb.Delete(id); } catch (SqlSugarException) { throw; } catch (Exception) { throw; } } /// /// 更新 /// /// /// public virtual bool Update(T obj) { try { return CurrentDb.Update(obj); } catch (SqlSugarException) { throw; } catch (Exception) { throw; } } } }