|
|
|
|
using NLog;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using DbType = SqlSugar.DbType;
|
|
|
|
|
namespace DS.WMS.PrintApi.Utils
|
|
|
|
|
{
|
|
|
|
|
public class SqlSugarUtil
|
|
|
|
|
{
|
|
|
|
|
static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 默认的Sql Server的链接字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static string ProTag = AppSetting.Configuration["ConnectionStrings:Pro"];
|
|
|
|
|
|
|
|
|
|
private static string DataType = AppSetting.Configuration["ConnectionStrings:DbType"];
|
|
|
|
|
|
|
|
|
|
public static SqlSugarClient GetInstance()
|
|
|
|
|
{
|
|
|
|
|
return GetInstance("1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SqlSugarClient GetInstance(string configId)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
|
|
|
|
|
{
|
|
|
|
|
new ConnectionConfig()
|
|
|
|
|
{
|
|
|
|
|
ConfigId = "1", DbType = DataType.ToEnum<DbType>(), ConnectionString = ProTag,
|
|
|
|
|
InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true
|
|
|
|
|
},
|
|
|
|
|
//new ConnectionConfig(){ ConfigId="2", DbType=DbType.SqlServer, ConnectionString=DevTag,InitKeyType=InitKeyType.Attribute,IsAutoCloseConnection=true,AopEvents=aop}
|
|
|
|
|
});
|
|
|
|
|
db.ChangeDatabase(configId);
|
|
|
|
|
//记录数据库配置
|
|
|
|
|
//Console.WriteLine("数据库配置:"+JsonConvert.SerializeObject(db.CurrentConnectionConfig));
|
|
|
|
|
//Logger.Log(LogLevel.Trace, JsonConvert.SerializeObject(db.CurrentConnectionConfig));
|
|
|
|
|
db.Ado.CommandTimeOut = 30;
|
|
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
{
|
|
|
|
|
Logger.Log(LogLevel.Info,
|
|
|
|
|
DateTime.Now.ToString() + "\r\n" +
|
|
|
|
|
UtilMethods.GetSqlString(DataType.ToEnum<DbType>(), sql, pars));
|
|
|
|
|
};
|
|
|
|
|
db.Aop.OnError = (exp) => //执行SQL 错误事件
|
|
|
|
|
{
|
|
|
|
|
// Console.WriteLine(DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
|
|
|
|
|
Logger.Error(DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
|
|
|
|
|
|
|
|
|
|
//Logger.Log(LogLevel.Error, DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres);
|
|
|
|
|
|
|
|
|
|
//exp.sql exp.parameters 可以拿到参数和错误Sql
|
|
|
|
|
};
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|