简体   繁体   中英

ADOX can't be used in VS2010?

I want to use ADOX to create a database in VS2010 but on using 'ADOX.catalogClass' i get the unusual error: Interop type 'ADOX.CatalogClass' cannot be embedded. Use the applicable interface instead. Actually the specific line being marked as an error is: ADOX.CatalogClass cat = new ADOX.CatalogClass();

Does ADOX not work for VS2010? How can i solve this? Thanks

You have the "Embed interop types" option set to True on the reference. Very nice feature, but it doesn't support using the XxxClass wrapper directly. Unintuitive, but you can create an instance of the COM interface with the new operator. Change your code to this to get rid of the error:

   ADOX.Catalog cat = new ADOX.Catalog();

I used ADOX in VS2010 with ADOX.Catalog, not CatalogClass. Here's an example of where I use it:

private void CreateAndExportLegacyFile(string exportFilePath)
{
    var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";
    connectionString += "Data Source=" + exportFilePath + ";Jet OLEDB:Engine Type=5";

    var catalog = new Catalog();
    catalog.Create(connectionString);

    var table = new Table { Name = "Main" };

    #region Column mapping
    table.Columns.Append("ID", DataTypeEnum.adVarWChar, 50);
    // Snipped rest of them
    #endregion

    foreach (Column column in table.Columns)
    {
        if (column.Name != "ID")
        {
            column.Attributes = ColumnAttributesEnum.adColNullable;
        }
    }

    catalog.Tables.Append(table);

    Marshal.FinalReleaseComObject(table);
    Marshal.FinalReleaseComObject(catalog.Tables);
    Marshal.FinalReleaseComObject(catalog.ActiveConnection);
    Marshal.FinalReleaseComObject(catalog);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM