5

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.

2
  • 1
    Put Console.WriteLine(String.Format("{0}-bit", IntPtr.Size * 8)) right at the beginning of your code. What does it report? Commented Jan 20, 2015 at 14:57
  • 64-bit. Forgot to tell the specs of my pc, sorry! Commented Jan 20, 2015 at 15:06

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.