Alarion
Alarion

Reputation: 153

Connecting to .accdb from C#. "Unrecognized database format" errox

I'm writing a short utility program for a client that will run a series of queries on an access database and will save the resulting data tables to an excel file. I'm having trouble establishing the initial connection to the access file.

The access queries are set up like so: The file I'm connecting to (let's call it Access1) has stored queries that I need to run. The tables that are queried reside in a separate access file (Access2). The program throws the exception when I try to open the connection.

Here is my connection method

    private static OleDbConnection GetConnection()
    {
        var connection = new OleDbConnection
        {
            ConnectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False", accessFilename)
        };

        connection.Open();

        return connection;
    }

I've followed the advice of some other answers (specifically here) but am still receiving the error.

I'm not entirely convinced that the problem is the code itself because I'm able to successfully run the program on about half the computers in our office. This makes me think its a driver or office version issue.

My machine (which won't run the program) is running Office 2010 64-bit and 64-bit Windows 7. To the best of my knowledge, this is the only version of windows/office that has been installed on this machine.

My supervisor's machine (which will run it) is running the exact same versions of both office and windows. In addition, I didn't have to install anything extra on his machine to run the program. His machine has been around the block a few times and probably had an earlier version of office either 2007 or 32-bit 2010 at some point. He can't remember exactly.

Other office/windows versions that it runs successfully on are -Windows 7 64-bit / Office 2007 32-bit -Windows XP / Office 2007 32-bit.

To add another wrinkle, the connection opens without a problem on my machine if I change the file name to any other access file. The queries all fail to run when I do that since it's a completely different file, but the connection opens just fine.

Since I can successfully connect to any other access file but the one I need, is there some setting in the one access file I need to change to allow an outside program to connect to it? I would think not since other computers can do that just fine.

Or is there some driver or reference or whatever that I'm overlooking and need to install in order to connect to MS Access? If so, how am I able to connect to other Access files?

Upvotes: 1

Views: 13888

Answers (2)

You can Try

Provider=Microsoft.Jet.OLEDB.4.0

or

Provider=Microsoft.ACE.OLEDB.12.0

according to the access file.this worked for me

Upvotes: 0

Chirag
Chirag

Reputation: 1851

After changing the Target Platform to "AnyCPU" it worked for me and was able to connect to .accdb database. I was using "x86" in order for me to change the code during debug on 64-bit machine. thanks DJ KRAZE!

Upvotes: 4

Related Questions