Beispiel #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);
        }