【问题标题】:Get Oracle Schema获取 Oracle 架构
【发布时间】:2012-08-29 12:20:05
【问题描述】:

有没有办法在 c# 中检索 oracle 架构? 这些是我需要的: Tables_Name, 表 Columns_Name, 主键, 唯一键, 外键

【问题讨论】:

    标签: c# oracle provider


    【解决方案1】:

    Table Names:

    select * from user_tables
    

    Table Column Names:

    select * from user_tab_columns
    

    Primary Keys, Unique Keys, Foreign Keys:

    select * from user_constraints
    

    More generally:

    select * from dictionary
    

    查看您可以使用的所有可能的系统视图。

    如果您想要用于创建表等的实际 DDL,您可以使用dbms_metadata.get_ddl,它会返回一个 CLOB。

    例如:

    select dbms_metadata.get_ddl('TABLE','MY_TABLE') from dual;
    

    【讨论】:

      【解决方案2】:

      您可以像选择任何其他表一样从 Oracle 中的数据字典表中进行选择。这是对数据字典表的讨论 - Data Dictionary

      【讨论】:

        【解决方案3】:

        使用DBMS_METADATA 包。 例如,要获取所有表的 CREATE 脚本,您可以:

        SELECT DBMS_METADATA.GET_DDL('TABLE', u.table_name)
          FROM USER_ALL_TABLES u
        

        通过这种方式,您只需稍加努力即可获得整个架构的 DDL 脚本。 DBMS_METADATA documetation page 上还有更多示例。

        【讨论】:

          【解决方案4】:

          我使用OracleConnection.GetSchema 方法。我为自己编写了一个帮助方法来检索每个表。

          private class SchemaInfo
          {
              public bool Mandatory { get; set; }
              public int MaxLength { get; set; }
          }
          
          private Dictionary<string, SchemaInfo> _getSchemaInfo(OracleConnection _cn, string owner, string tableName)
          {
              DataTable _dt = _cn.GetSchema("Columns", new string[3] { owner, tableName, null });
              Dictionary<string, SchemaInfo> _dict = new Dictionary<string, SchemaInfo>();
              for (int x = 0; x < _dt.Rows.Count; x++)
              {
                  DataRow _r = _dt.Rows[x];
                  SchemaInfo _si = new SchemaInfo();
                  object maxl = _r.ItemArray[10];
                  if (maxl == DBNull.Value) maxl = -1;
                  _si.Mandatory = (_r.ItemArray[8].ToString().Equals("N")) ? true : false;
                  _si.MaxLength = Convert.ToInt32((maxl ?? 0));
                  _dict.Add(_r.ItemArray[2].ToString(), _si);
              }
              return _dict;
          }
          

          您必须查找返回的其他元素以获取密钥等。我很确定这是可用的(但可能是错误的)。

          如果要获取表格,请使用Connection.GetSchema("Tables", new string[3] { owner, null, null });

          【讨论】:

            猜你喜欢
            • 2022-01-21
            • 2012-09-22
            • 2018-12-03
            • 2010-09-15
            • 2020-06-30
            • 1970-01-01
            • 2015-01-30
            • 1970-01-01
            • 2021-12-17
            相关资源
            最近更新 更多