0

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?
NECben067
  • 427
  • 1
  • 4
  • 20
  • 1
    _I also installed the newer driver which should support 32 and 64 bit systems_. That's quite an achievement, because no such driver exists as far as I know. You can either have the 32-bit driver or the 64-bit one, but not both at the same time, and none exist that support both. – Erik A Sep 01 '20 at 09:55
  • I installed the driver referred in the last answer: https://stackoverflow.com/questions/1735392/the-microsoft-jet-oledb-4-0-provider-is-not-registered-on-the-local-machine Maybe I didn't do it right, I dont know. – NECben067 Sep 01 '20 at 10:04
  • That should give you a choice of the 32-bit and 64-bit driver, which can't be installed simultaneously on the same system. Also, it's the ACE driver, not the JET one, which can open MDB files too but you need to use its name: `Provider=Microsoft.ACE.OLEDB.12.0;`. The 64-bit version is incompatible with 32-bit programs. – Erik A Sep 01 '20 at 10:24
  • hmm ok, i tried it with ```Provider=Microsoft.ACE.OLEDB.12.0;``` already. I didn't have any other driver installed before I installed this driver. What do I have to do exactly to get the 32-bit version? There was no option to choose or anything – NECben067 Sep 01 '20 at 10:40
  • Huh? On the link there (bottom answer), there are two downloads, one with `_x64` in the name. That's the 64-bit version, the other is the 32-bit version. Make sure to install all traces of MS Access before installing a different bitness version – Erik A Sep 01 '20 at 10:54
  • The original post was with the 32-bit version already being installed. I uninstalled it now and tried the x64-version and I think I got a little further. But now I get this error: ```Unhandled exception. System.Data.OleDb.OleDbException (0x80004005): Unrecognized database format 'C:\Users\but\Desktop\Access-DB\Test\access\remote.mdb'.``` It is really annoying because it seems that this old database file cannot be read... But it shouldn't be corrupted because I can open it with an MDBReader and see all tables and records. – NECben067 Sep 01 '20 at 11:14
  • I would consider using some third party tool to convert the file to some other kind of format that is easier to interact with, like a MsSql or csv files. Working with access files tend to be error-prone in my experience. – JonasH Sep 01 '20 at 11:29
  • @JonasH that would for sure help, do you know any that I could automate with any programming language? I have seen converters, where u must convert the acces db manually but that wouldnt help, because I have to read from this file constantly – NECben067 Sep 01 '20 at 11:31
  • @NECben067 When I did something similar I used the ACE driver to convert the data to a custom file-based format. The new format where then used to recreate access files for the customers who needed them, but that process failed regularly for unknown reasons. I have no specific recommendations other that avoiding access as much as possible. – JonasH Sep 01 '20 at 12:03
  • @NECben067 - It sounds like you *thought* your app was running as 32-bit when really it wasn't. `String.Format("{0}-bit", IntPtr.Size * 8)` will show you what's really going on. – Gord Thompson Sep 01 '20 at 14:21

1 Answers1

1

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.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • First of all, thank you for your very detailed answer! I will try it out immediately :) But this isn't visual studio code correct? Ist this normal visual studio? Because I don't have a settings interface like on the pictures shown. – NECben067 Sep 02 '20 at 06:33
  • It is "normal" Visual Studio. – Gustav Sep 02 '20 at 07:16
  • Well, I did not write any code just yet (and that WAS much my point - use VS to setup and test the connection before you actually write any code). So yes, the above screen shots are from standard Visual Studio (2019 - free express edition). – Albert D. Kallal Sep 02 '20 at 17:58