Ejemplo n.º 1
0
        public static Database FromConnection(SqlConnection connection)
        {
            Database database = null;

            Dictionary<ObjectName, Table> tables = new Dictionary<ObjectName, Table>();

            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = connection;
                command.CommandText =
                    "SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES";

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // Create the database upon reading the first row so that the database name
                        // can be set using the canonical capitalisation.
                        if (database == null) database = new Database(reader.GetString(0));

                        Table table = new Table(database, new ObjectName(reader.GetString(1), reader.GetString(2)));
                        tables[table.Name] = table;
                    }
                }
            }

            foreach (Table table in tables.Values)
            {
                table.LoadColumnsFromDatabase(connection);
            }

            database._tables = tables;
            database._tablesByShortName = new Dictionary<string, Table>();

            foreach (Table table in tables.Values)
            {
                database._tablesByShortName[table.ParsedName.Value] = table;
            }

            Constraint.PopulateTableConstraintsFromConnection(connection, database);

            return database;
        }