You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6 KiB
C#

using AUCMA.STORE.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AUCMA.STORE.SqlSugar
{
public class SqlGenerator
{
public static string ConnectionString = ConfigHelper.GetConfig("MesOracleDataSource");
private static SqlSugarClient sqlSugarClient = null;
private static readonly object lockHelper = new object();
/// <summary>
/// 连接Oracle数据库
/// </summary>
/// <returns></returns>
public static SqlSugarClient GetOracleInstance()
{
if (sqlSugarClient == null)
{
lock (lockHelper)
{
//此写法,官方不推荐
//单例模式,避免多次实例化
if (sqlSugarClient == null)
{
sqlSugarClient = new SqlSugarClient(new ConnectionConfig()
{
//数据库类型
DbType = DbType.Oracle,
//数据库连接字符串
ConnectionString = ConnectionString,
InitKeyType = InitKeyType.Attribute,
//是否自动关闭连接
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
//记录执行SQL和参数
OnLogExecuting = (sql, p) =>
{
//log.Debug(sql);
//Console.WriteLine(sql);
}
}
});
}
}
}
return sqlSugarClient;
}
//官方建议 不单例 SqlSugarClient 对象 ,在多线程下,可能会引发异常。
public static SqlSugarClient GetSQLServerInstance()
{
return new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = ConnectionString,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (sql, p) =>
{
//log.Debug(sql);
}
}
});
}
}
}