public static ISqlSugarClient GetSqlSugarClient(InitKeyType keyType = InitKeyType.SystemTable)
 {
     return(new SqlSugar.SqlSugarClient(new SqlSugar.ConnectionConfig()
     {
         ConnectionString = DBConfig.ConnectionString, //必填, 数据库连接字符串
         DbType = (SqlSugar.DbType)DBConfig.DbType,    //必填, 数据库类型
         IsAutoCloseConnection = true,                 //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
         InitKeyType = keyType                         //默认SystemTable, 字段信息读取, 如:该属性是不是主键,标识列等等信息
     }));
 }
示例#2
0
        /// <summary>
        /// 创建连接
        /// </summary>
        /// <param name="connName"></param>
        /// <param name="initKeyType"></param>
        /// <param name="entityNameService"></param>
        /// <param name="entityService"></param>
        /// <returns></returns>
        public SqlSugarClient CreateConnection(string connName, InitKeyType initKeyType = InitKeyType.Attribute, Action <Type, EntityInfo> entityNameService = null, Action <PropertyInfo, EntityColumnInfo> entityService = null)
        {
            var config = _configuration.Get <Config>();

            if (config == null)
            {
                throw new NotFoundException("缺少SqlSugar配置");
            }
            if (!config.ConnectionConfigs.ContainsKey(connName))
            {
                throw new Exception("找不到该连接的配置");
            }
            var connConfig       = config.ConnectionConfigs[connName];
            var connectionConfig = new ConnectionConfig()
            {
                ConnectionString          = connConfig.ConnectionString,    //必填, 数据库连接字符串
                DbType                    = connConfig.DbType,              //必填, 数据库类型
                IsAutoCloseConnection     = true,                           //默认false, 自动关闭数据库连接, 设置为true无需使用using或者Close操作
                InitKeyType               = initKeyType,                    //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
                ConfigureExternalServices = new ConfigureExternalServices() // 配置扩展服务
            };

            if (connConfig.UseMemoryCache)
            {
                connectionConfig.ConfigureExternalServices.DataInfoCacheService = new DataMemoryCache();                            // Memory缓存
            }
            // 主从模式
            if (connConfig.SlaveConnections != null && connConfig.SlaveConnections.Length > 0)
            {
                connectionConfig.SlaveConnectionConfigs = new List <SlaveConnectionConfig>();
                foreach (var item in connConfig.SlaveConnections)
                {
                    connectionConfig.SlaveConnectionConfigs.Add(new SlaveConnectionConfig()
                    {
                        ConnectionString = item.ConnectionString,
                        HitRate          = item.HitRate
                    });
                }
            }
            if (entityNameService != null)
            {
                connectionConfig.ConfigureExternalServices.EntityNameService = entityNameService;
            }
            if (entityService != null)
            {
                connectionConfig.ConfigureExternalServices.EntityService = entityService;
            }
            var db = new SqlSugarClient(connectionConfig);

            if (db == null)
            {
                throw new Exception("无法创建连接");
            }
            return(db);
        }
示例#3
0
        public SqlSugarClient Instence(bool isEnableLog = false, InitKeyType type = InitKeyType.SystemTable)
        {
            var sugarClient = new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString      = ConnectionString,
                DbType                = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType           = type
            });

            if (isEnableLog)
            {
                WriteSqlLog(sugarClient);
            }

            return(sugarClient);
        }
示例#4
0
        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="isAutoCloseConnection">(默认false)自动释放数据务,如果存在事务,在事务结束后释放</param>
        /// <param name="initKeyType">(默认SystemTable)从实体特性中读取主键自增列信息</param>
        /// <param name="isOnLogExecuting">是否打印SQL</param>
        /// <returns></returns>
        public static bool ContectedDataBase(DbType dbType = DbType.SqlServer, bool isAutoCloseConnection = true, InitKeyType initKeyType = InitKeyType.Attribute, bool isOnLogExecuting = false)
        {
            try
            {
                if (!string.IsNullOrEmpty(_connectionString))
                {
                    _db = new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString      = _connectionString, // 连接字符串
                        DbType                = dbType,
                        IsAutoCloseConnection = isAutoCloseConnection,
                        InitKeyType           = initKeyType,
                    });

                    // 打印SQL调式
                    if (isOnLogExecuting)
                    {
                        _db.Aop.OnLogExecuting = (sql, pars) =>
                        {
                            Console.WriteLine(sql + "\r\n" + _db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                        };
                    }

                    return(true);
                }
                return(false);
            }
            catch
            {
                return(false);
            }
        }
示例#5
0
 /// <summary>
 /// 创建连接
 /// </summary>
 /// <param name="initKeyType"></param>
 /// <param name="entityNameService"></param>
 /// <param name="entityService"></param>
 /// <returns></returns>
 public SqlSugarClient CreateConnection(
     InitKeyType initKeyType = InitKeyType.Attribute,
     Action <Type, EntityInfo> entityNameService           = null,
     Action <PropertyInfo, EntityColumnInfo> entityService = null
     ) => _sqlSugarService.CreateConnection(_connName, initKeyType, entityNameService, entityService);