예제 #1
0
        public DbDataSourceSchema GetSchema(DbConnectionString connectionString)
        {
            var provider = DbProvider.GetRegisteredProvider(connectionString.DbProvider.InvariantName);

            if (provider is null)
            {
                return(null);
            }

            var databases = new List <string>();

            using var sqlConnection = provider.CreateConnection();
            if (sqlConnection is null)
            {
                return(null);
            }

            sqlConnection.ConnectionString = connectionString.ToString();
            sqlConnection.Open();

            using var command   = sqlConnection.CreateCommand();
            command.Connection  = sqlConnection;
            command.CommandText = "SELECT name from sys.databases";
            command.CommandType = CommandType.Text;

            using var dataReader = command.ExecuteReader();
            while (dataReader.Read())
            {
                databases.Add(dataReader[0].ToString());
            }

            return(new DbDataSourceSchema {
                Databases = databases
            });
        }
        public static ConnectionState GetConnectionState(this DbConnectionString connectionString)
        {
            var connectionStringStr = connectionString?.ToString();

            if (string.IsNullOrWhiteSpace(connectionStringStr))
            {
                return(ConnectionState.Invalid);
            }

            var connection = connectionString.DbProvider.GetProvider()?.CreateConnection();

            if (connection is null)
            {
                return(ConnectionState.Invalid);
            }

            // Try to open
            try
            {
                connection.ConnectionString = connectionStringStr;
                connection.Open();
            }
            catch
            {
                return(ConnectionState.Invalid);
            }
            finally
            {
                connection.Dispose();
            }

            return(ConnectionState.Valid);
        }
        public static DbDataSourceSchema GetDataSourceSchema(this DbConnectionString connectionString)
        {
            Argument.IsNotNull(() => connectionString);

            var provider       = connectionString.DbProvider;
            var schemaProvider = provider.GetProvider()?.GetOrCreateConnectedInstance <IDataSourceSchemaProvider>();

            return(schemaProvider?.GetSchema(connectionString));
        }
        public static DbConnectionStringProperty TryGetProperty(this DbConnectionString connectionString, string propertyName)
        {
            var properties = connectionString?.Properties;

            if (properties is null)
            {
                return(null);
            }

            var upperInvariantPropertyName = propertyName.ToUpperInvariant();

            if (properties.TryGetValue(upperInvariantPropertyName, out var property))
            {
                return(property);
            }

            if (properties.TryGetValue(upperInvariantPropertyName.Replace(" ", string.Empty), out property))
            {
                return(property);
            }

            return(null);
        }