Search code examples
c#winformsms-accessoledb

Unrecognized Database Format While trying to open access 97 database


I created a mdb (Access Database 97) file with that code ,

  string DBPath = @"C:\\Users\\Desktop\\test.mdb";

        // create DB via ADOX if not exists
        if (!File.Exists(DBPath))
        {
            ADOX.Catalog cat = new ADOX.Catalog();
            cat.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DBPath);
            cat = null;
        }

        // connect to DB
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DBPath);
        con.Open();

When I am trying to open created database it gives an "unrecognized database format" error.

I am using Microsoft Access 97 , I guess it is 32 bit. I tried all platform targets(x86,x64 & Any CPU) from C# but still same problem.

The both access 97 and Microsoft Office 2010 installed in my computer.

The error is like that;

enter image description here

Could you please help me ?


Solution

  • After conducting deep research in hours, I found 2 main issues regarding database connection:

    1. Using backslash escape sequences in a string literal doesn't converting escape sequence character, instead you should use one backslash for file path:

      string DBPath = @"C:\Users\Desktop\test.mdb";
      
    2. The connection string provider to connect with Access database in question uses Microsoft ACE OLE DB 12.0, which only supported by Access 2007 and later with ACCDB format (use Microsoft Jet 4.0 provider instead).

      Additionally, in database creation part which includes ADOX.Catalog.Create() method, it should include Jet OLEDB:Engine Type parameter to specify which Access version being used. By default it sets to Jet OLEDB:Engine Type=5 which means Access 2000 file format (will trigger unrecognized database format error in Access 97), hence to force ADOX creating MDB with Access 97 format it is necessary to set Engine Type=4 like below:

      // database creation
      if (!File.Exists(DBPath))
      {
          ADOX.Catalog cat = new ADOX.Catalog();
          cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath + ";Jet OLEDB:Engine Type=4");
          // other stuff
      }
      
      // connect to database using Jet OLE DB provider
      OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBPath);
      

    Additional reference:

    Unrecognized MDB created by ADOX