Search code examples
c#ms-accessvisual-studio-codeoledb

Setting target to x86 in Visual studio code for 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine error


I am new to C# and only need to use it because I want to read a really old MS Access 95 database file.

When I try to read the mdb file I get the The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine. error. I read through some threads and know, that it probably is because of my OS being 64-bit and the driver being 32 bit. I also installed the newer driver which should support 32 and 64 bit systems, but I don't know what I exactly should do after installing it.

This is my code so far:

using System.Data.OleDb;

namespace Access_DB
{
    class Program
    {
        public static void Main(string[] args)
        {
            //Console.WriteLine("\nWhat is your name? ");
            ReadData("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\Users\\but\\Desktop\\Access-DB\\Test\\access" , "select * from summary_0199_550");
        }

        public static void ReadData(string connectionString, string queryString)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(queryString, connection);

                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Console.WriteLine(reader[0].ToString());
                }
                reader.Close();
            }
        }
    }
}``

I also read that I should force the application to run on 32 bit by setting the target to x86 in project properties, but I cannot find such an option in visual studio code. How do I change the target here?

Solution

  • Well, given this is a mdb file? Then use JET as opposed to the newer ACE. The reasons are many, but one nice reason is that JET been installed on every computer since windows 98. So you don't have to install anything.

    Next up, why not use the connection builder in VS? So, under settings, use this:

    enter image description here

    And when you click on the above [...] connection builder? You get this:

    You CAN CHOOSE JET by selecting this: enter image description here

    When you click on advanced, you get a chance to choose jet or ACE. Since this is a older mdb file, then NO NEED to bother with ACE.

    So, on advanced, you get this choice:

    enter image description here

    So, in above, you can (and SHOULD) choose jet.

    Now, once you done all the above? Surely you not guessing, Surely you not running code, Surely you spent that huge 5 seconds, and clicked on the Test connection button, right?

    this one:

    enter image description here

    So, you have used test connection, right? Or how were you testing this connection?

    Ok, next up: Forcing your project to x86. Well, "any cpu" will by default run as x32 bits WHEN launched from Visual Studio (since Visual Studio is a x32 bit program). However, if you launch the .exe from the windows command prompt? Then you get a x64 bit in-process program, and you CAN NOT use JET.

    So, yes, you should force your project to x86

    from VS: enter image description here

    Then choose new from this:

    enter image description here

    And then this: enter image description here

    Now you ahave two options - "any cpu" and x86. You MOST certainly want to force this project to x86.

    You have the options here now

    enter image description here

    Now, if the mdb file is AFTER access 2003, then you might want to choose ACE, but then again, spending 15 seconds using connection builder and hitting a test button will rather quick help you decide if jet can open + use the file.