【问题标题】:How do I list the table names in a database?如何列出数据库中的表名?
【发布时间】:2010-09-21 15:50:35
【问题描述】:

我们有一个我只有读取权限的 Oracle 8i 数据库。我们使用 ODBC 和 MS Access 从该数据库读取数据,因为我们没有 Oracle 客户端软件。这工作正常。 我将 ADO.NET 与 ASP.NET 一起使用。现在我想通过 ODBC 显示我在 MS Access 中看到的所有表的列表。 我已经在 C# 中使用 ODBC 连接进行了尝试。我尝试了以下查询来获取表列表,但没有成功。

  1. 从 dba_tables 中选择表名;
  2. 从所有表中选择表名;
  3. 从选项卡中选择 tname;

请帮忙。

感谢您的回复。我没有运气就试过了。我只想看到当我使用 ODBC 创建链接表时 MS Access 中可用的相同表列表。

这是我用来实现此目的的功能,它并没有真正按照我想要的方式工作。

public static ArrayList GetODBCTablesList()
        {
            try
            {                
                OdbcConnection DbConnection = new OdbcConnection("DSN=mydsn;UID=user1;PWD=pwd1;");
                DbConnection.Open();

                OdbcCommand DbCommand = DbConnection.CreateCommand();
                DbCommand.CommandText = "select table_name from all_tables";                
                OdbcDataReader DbReader = DbCommand.ExecuteReader();

                if (DbReader != null)
                {
                    ArrayList TableList = new ArrayList();
                    while (DbReader.Read())
                    {
                        TableList.Add(DbReader.GetString(0));
                    }
                    DbReader.Close();
                    DbCommand.Dispose();
                    DbConnection.Close();

                    TableList.Sort();
                    TableList.TrimToSize();
                    return TableList;
                }

                DbCommand.Dispose();
                DbConnection.Close();

                return null;
            }
            catch (Exception ex)
            {
                LogHandler.WriteLogMessage(ex.GetBaseException().ToString(), true);
                return null;
            }
        }

这给了我一个表列表,其中不包含我在使用 ODBC 链接 MS Access 中的表时看到的所有表。

【问题讨论】:

  • 您能否详细介绍一下查询 2 的问题?
  • 也许你应该再试一次,但这次,运气好

标签: c# oracle ado.net


【解决方案1】:

这行得通:

select table_name from tabs;

【讨论】:

    【解决方案2】:

    你可以试试 select table_name from user_tables

    select object_name from USER_objects where object_type='TABLE'

    【讨论】:

      【解决方案3】:

      由于您使用的是 ADO.NET,我建议您使用 OdbcConnection.GetSchema。此方法返回一个DataTable,其中包含有关数据库架构的信息。

      来自this answer,这可能对你有用:

      OdbcConnection.GetSchema("表")

      【讨论】:

        【解决方案4】:

        试试:

        SELECT owner, table_name
          FROM dba_tables
        

        取自Get list of all tables in Oracle?

        【讨论】:

        • 我读到了一些关于 dba_tables 受到某种许可的内容。但我不记得那是什么。
        猜你喜欢
        • 1970-01-01
        • 2020-04-28
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-12
        • 2017-09-16
        相关资源
        最近更新 更多