C# 获取Oracle架构

C# 获取Oracle架构,c#,oracle,provider,C#,Oracle,Provider,有没有办法在c中检索oracle模式? 这些是我需要的: 表格名称:, 表列的名称, 主键, 独特的钥匙, 外键您可以从Oracle中的数据字典表中进行选择,就像选择任何其他表一样。以下是对数据字典表的讨论-: : : : 要查看您可以使用的所有可能的系统视图 如果您想要用于创建表等的实际DDL,您可以使用它,它返回CLOB 例如: select dbms_metadata.get_ddl('TABLE','MY_TABLE') from dual; 使用DBMS_元数据包。 例如,要获取所有

有没有办法在c中检索oracle模式? 这些是我需要的: 表格名称:, 表列的名称, 主键, 独特的钥匙,
外键

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

:

:

:

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

如果您想要用于创建表等的实际DDL,您可以使用它,它返回CLOB

例如:

select dbms_metadata.get_ddl('TABLE','MY_TABLE') from dual;
使用DBMS_元数据包。 例如,要获取所有表的创建脚本,您需要执行以下操作:

SELECT DBMS_METADATA.GET_DDL('TABLE', u.table_name)
  FROM USER_ALL_TABLES u
这样,只需稍加努力,就可以获得整个模式的DDL脚本。还有更多关于的示例。

我使用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.GetSchemaTables,新字符串[3]{owner,null,null}

select * from dictionary
select dbms_metadata.get_ddl('TABLE','MY_TABLE') from dual;
SELECT DBMS_METADATA.GET_DDL('TABLE', u.table_name)
  FROM USER_ALL_TABLES u
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;
}