예제 #1
0
        private static string AlterConnectionStringPropertyValue(this string connectionString, string providerName, Func <string, string> alteractionFunction)
        {
            Argument.IsNotNullOrEmpty(() => connectionString);
            Argument.IsNotNullOrEmpty(() => providerName);
            Argument.IsNotNull(() => alteractionFunction);

            var provider           = DbProvider.GetRegisteredProviders()[providerName];
            var dbConnectionString = provider.CreateConnectionString(connectionString);

            if (dbConnectionString is null)
            {
                return(connectionString);
            }

            var connectionStringBuilder = dbConnectionString.ConnectionStringBuilder;
            var sensitiveProperties     = dbConnectionString.Properties.Where(x => x.Value.IsSensitive);

            foreach (var sensitiveProperty in sensitiveProperties)
            {
                var value = connectionStringBuilder[sensitiveProperty.Key].ToString();
                if (!string.IsNullOrWhiteSpace(value))
                {
                    connectionStringBuilder[sensitiveProperty.Key] = alteractionFunction(value);
                }
            }

            return(connectionStringBuilder.ConnectionString);
        }
예제 #2
0
        public static string GetConnectionStringProperty(this string connectionString, string providerName, string propertyName)
        {
            Argument.IsNotNullOrEmpty(() => connectionString);
            Argument.IsNotNullOrEmpty(() => providerName);

            var provider           = DbProvider.GetRegisteredProviders()[providerName];
            var dbConnectionString = provider.CreateConnectionString(connectionString);

            if (dbConnectionString is null)
            {
                return(connectionString);
            }

            if (dbConnectionString.Properties.TryGetValue(propertyName, out var dataSourceProperty))
            {
                return(dataSourceProperty.Value?.ToString() ?? string.Empty);
            }

            return(null);
        }
        private static DbProvider GetProviderByConnectionType(Type connectionType)
        {
            Argument.IsNotNull(() => connectionType);

            if (ConnectionTypeToProvider.TryGetValue(connectionType, out var dbProvider))
            {
                return(dbProvider);
            }

            var dbProviders = DbProvider.GetRegisteredProviders();

            foreach (var currentProvider in dbProviders.Values)
            {
                if (currentProvider.ConnectionType == connectionType)
                {
                    ConnectionTypeToProvider[connectionType] = currentProvider;
                    return(currentProvider);
                }
            }

            return(null);
        }