Ejemplo n.º 1
0
        /// <summary>
        /// Sql CRUD provider and helper
        /// </summary>
        /// <param name="key">ConnectionString key in config</param>
        /// <param name="driverPath">Your driver path,the default path is strat up dir</param>
        public SqlHelper(string key, string driverPath = null)
        {
            if (!IntrospectionManager.IsExistsKey(key))
            {
                var exception = new ConnectionStringKeyNotExists(key);
                throw exception;
            }
            _key = key;
            _dbConnectionString = IntrospectionManager.GetConnectionString(key);
            _providerName       = IntrospectionManager.GetProviderName(key);

            setDatabaseType();
            setDatabaseName();
            if (driverPath == null)
            {
                setDriverAssembly(AppDomain.CurrentDomain.BaseDirectory);
            }
            else
            {
                setDriverAssembly(driverPath);
            }
            if (_dataBaseType == DataBaseNames.Sqlite)
            {
                var builder = new DbConnectionStringBuilder();
                builder.ConnectionString = _dbConnectionString;
                object database = "";
                builder.TryGetValue("Data Source", out database);
                if (!System.IO.File.Exists(database.ToString()))
                {
                    string accessPath = AppDomain.CurrentDomain.BaseDirectory + database.ToString();
                    if (System.IO.File.Exists(accessPath))
                    {
                        _dbConnectionString = "Data Source=" + accessPath;
                    }
                }
            }
            if (_driverAssembly != null)
            {
                string info = string.Format("[Prvider:'{0}'][Key:'{1}'][Db:'{2}'] {3}", _dataBaseType, _key, _dataBaseName, "Create sqlhelper instance sucessed!");
                LogHelper.LogInfo(info);
            }
            else
            {
                string info = string.Format("[Prvider:'{0}'][Key:'{1}'][Db:'{2}'] {3}", _dataBaseType, _key, _dataBaseName, "Create sqlhelper instance fail!");
                LogHelper.LogError(info);
            }
        }