从 access 97 数据库获取 tables/schema

Get tables/schema from access 97 database

我有一个 Access 97 数据库,我正试图从中获取数据架构和数据。我不知道有多少 table,也不知道它们叫什么,也不知道有什么列名。

我以编程方式进入数据库没问题,但您如何发现架构?

我正在使用它来获取架构 table:

static DataTable GetSchemaTable(string connectionString)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        connection.Open();
        DataTable schemaTable =
            connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
            new object[] { null, null, null, "TABLE" });
        return schemaTable;
    }
}

你的方向不错 对于每个 table,您可以再次调用 GetOleDbSchemaTable,但在这种情况下,使用不同的参数:

    DataTable schemaColumns =
        connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
        new object[] { null, "MyTableName", null, null });

您可以使用 OleDbSchemaGuid 指定检索哪个模式,并使用过滤器指定 table 模式

this link 中,您可以找到有关如何过滤的信息,例如通过 table 名称检索列信息。限制是一个数组,你应该把过滤值放在它对应的位置

GetOleDbSchemaTable with OleDbSchemaGuid.Tables field returns tables (including view names) defined inside catalog, while the object array refers to this construction:

new object { "table_catalog", "table_schema", "table_name", "table_type" }

OleDbSchemaGuid 由 3 个字段组成:OleDbSchemaGuid.TablesOleDbSchemaGuid.ColumnsOleDbSchemaGuid.Primary_Keys。要获得 table 属性,您可以使用 OleDbSchemaGuid.Columns 字段:

connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
        new object[] { "table_catalog", "table_schema", "table_name", "column_name" });

由于要查找 table 架构,设置第二个参数并将另一个参数保留为空值:

var columns = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
        new object[] { null, "schema_name", null, null });

如果您想通过 table 架构和 table 名称获取属性,也可以使用第三个参数:

var columns = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
        new object[] { null, "schema_name", "table_name", null });