Npgsql.NpgsqlConnection.GetSchema C# (CSharp) Method

GetSchema() public method

Returns the schema collection specified by the collection name filtered by the restrictions.
public GetSchema ( string collectionName, string restrictions ) : DataTable
collectionName string The collection name.
restrictions string /// The restriction values to filter the results. A description of the restrictions is contained /// in the Restrictions collection. ///
return System.Data.DataTable
        public override DataTable GetSchema(string collectionName, string[] restrictions)
        {
            using (var tempConn = new NpgsqlConnection(ConnectionString))
            {
                switch (collectionName)
                {
                    case "MetaDataCollections":
                        return NpgsqlSchema.GetMetaDataCollections();
                    case "Restrictions":
                        return NpgsqlSchema.GetRestrictions();
                    case "DataSourceInformation":
                        return NpgsqlSchema.GetDataSourceInformation();
                    case "DataTypes":
                        throw new NotSupportedException();
                    case "ReservedWords":
                        return NpgsqlSchema.GetReservedWords();
                        // custom collections for npgsql
                    case "Databases":
                        return NpgsqlSchema.GetDatabases(tempConn, restrictions);
                    case "Tables":
                        return NpgsqlSchema.GetTables(tempConn, restrictions);
                    case "Columns":
                        return NpgsqlSchema.GetColumns(tempConn, restrictions);
                    case "Views":
                        return NpgsqlSchema.GetViews(tempConn, restrictions);
                    case "Users":
                        return NpgsqlSchema.GetUsers(tempConn, restrictions);
                    case "Indexes":
                        return NpgsqlSchema.GetIndexes(tempConn, restrictions);
                    case "IndexColumns":
                        return NpgsqlSchema.GetIndexColumns(tempConn, restrictions);
                    case "ForeignKeys":
                        return NpgsqlSchema.GetForeignKeys(tempConn, restrictions);
                    default:
                        throw new ArgumentOutOfRangeException("collectionName", collectionName, "Invalid collection name");
                }
            }
        }

Same methods

NpgsqlConnection::GetSchema ( ) : DataTable
NpgsqlConnection::GetSchema ( string collectionName ) : DataTable

Usage Example

示例#1
0
文件: Program.cs 项目: elpy/DynORM
        static void Main(string[] args)
        {
            var b = new NpgsqlConnectionStringBuilder() { Host = "milkyway", Port = 5433, Username = "******", Password = "******", Database = "db" };

            using (DbConnection conn = new NpgsqlConnection(b.ToString()))
            {
                conn.Open();

                var tables = new List<string>();
                var all = conn.GetSchema("Tables");
                foreach (DataRow r in all.Rows)
                {
                    //table_catalog //table_schema //table_name //table_type
                    var schema = r["table_schema"];
                    var table = r["table_name"];
                    var type = r["table_type"];

                    if ("kernel".Equals(schema.ToString()))
                        tables.Add(table.ToString());
                }

                foreach (var table in tables)
                {
                    Console.WriteLine("Table: " + table);

                    var tableSchema = conn.GetSchema("Columns", new string[] { null, null, table });
                    foreach (DataRow row in tableSchema.Rows)
                    {
                        Console.WriteLine("Column = {0}. Type = {1}. Default = {2}. Nullable = {3}. Text lenght = {4}. Numeric precision = {5}.",
                            row["column_name"],
                            row["data_type"],
                            row["column_default"],
                            row["is_nullable"],
                            row["character_maximum_length"],
                            row["numeric_precision"]);
                    }
                }

                /*var cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"SELECT * FROM kernel.users;";

                using (var reader = cmd.ExecuteReader())
                    while (reader.Read())
                        Console.WriteLine(string.Format("id = {0}, user = {1};", reader.GetString(0), reader.GetString(1)));
                */

            }

            Console.ReadKey();
        }
All Usage Examples Of Npgsql.NpgsqlConnection::GetSchema