How to extract the schema of an Access (.mdb) database?

后端 未结 10 2527
星月不相逢
星月不相逢 2020-11-30 19:39

I am trying to extract the schema of an .mdb database, so that I can recreate the database elsewhere.

How can I pull off something like this?

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 19:52

    The following C# outlines how to obtain the schema from a .mdb file.

    Obtain a connection to the database:

    String f = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "database.mdb";
    OleDbConnection databaseConnection = new OleDbConnection(f);
    databaseConnection.Open();
    

    Get the name of each table:

    DataTable dataTable = databaseConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
    int numTables = dataTable.Rows.Count;
    for (int tableIndex = 0; tableIndex < numTables; ++tableIndex)
    {
        String tableName = dataTable.Rows[tableIndex]["TABLE_NAME"].ToString();
    

    Get the fields for each table:

        DataTable schemaTable = databaseConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, tableName, null });
        foreach (DataRow row in schemaTable.Rows)
        {
            String fieldName = row["COLUMN_NAME"].ToString(); //3
            String fieldType = row["DATA_TYPE"].ToString(); // 11
            String fieldDescription = row["DESCRIPTION"].ToString(); //27
        }
    }
    

    Where do the 3, 11 and 27 come from? I found them by inspecting DataRow.ItemArray with a debugger, does anyone know the 'correct' way?

提交回复
热议问题