Search code examples
c#databasems-accessadox

COM Exception: "Class not registered" when trying to create an ADOX.Catalog


I have been trying to create an Access database via C#. I have already tried using code from this post and this post, which I have to admit, have been very helpful. Right now this is the part of my code which should create the Access database:

 public bool CreateNewAccessDatabase(string fileName)
    {
            //var cat = new ADOX.Catalog();
            //using var instead of CatalogClass hasn't worked either.
            ADOX.CatalogClass cat = new ADOX.CatalogClass();
            string tmpStr;
            string filename = fileName;
            tmpStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
            tmpStr += "Data Source=" + filename + ";Jet OLEDB:Engine Type=5";
            cat.Create(tmpStr);
        Console.Out.WriteLine("OK");

        Table nTable = new Table();
        nTable.Name = "NombreDeEmpresa";
        nTable.Columns.Append("CAMPO1", ADOX.DataTypeEnum.adVarWChar, 25);
        nTable.Columns.Append("CAMPO2", ADOX.DataTypeEnum.adVarWChar, 45);
        nTable.Columns.Append("CAMPO3", ADOX.DataTypeEnum.adVarWChar, 45);
        nTable.Columns.Append("CAMPO4", ADOX.DataTypeEnum.adVarWChar, 45);
        nTable.Columns.Append("CAMPO5", ADOX.DataTypeEnum.adVarWChar, 25);
        nTable.Columns.Append("CAMPO6", ADOX.DataTypeEnum.adVarWChar, 20);
        nTable.Columns.Append("CAMPO7", ADOX.DataTypeEnum.adVarWChar, 15);
        cat.Tables.Append(nTable);

        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(nTable);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.Tables);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat.ActiveConnection);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cat);

        return true;
     }

However, if I debug the code, I always get the same exception when I reach the line that says:

cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;");    

All the information that I can retrieve from the Exception can be seen here (Sorry for having my Visual Studio in Spanish, I think the info is clear anyways). Oh, and I do have ADOX and adodb in my references. Does anybody know how can I solve this problem?

I'm using VS10, running on Windows 7 x64.


Solution

  • The older Microsoft "Jet" drivers are only available to 32-bit applications. If you want to use Jet you'll need to go into the "Build" tab of the Properties for your Visual Studio project and set the "Target platform" to "x86". That will force your application to run as 32-bit.