0

I am writing a C# executable and need to check if a given password for a Microsoft Access database is correct. It needs to be able to do this for both .mdb files and .accdb files. For .mdb I am using JET OLED and it works fine, but JET OLEDB doesn't support the newer versions of Microsoft Access so I use ACE OLEDB, but I get an error every time. Here is the relevant method:

public int CheckPassword(string password, string filePath)
    {
        // Ensure the correct provider for with .mdb(default) or .accdb
        string providerName = "Microsoft.Jet.OLEDB.4.0";

        if (Path.GetExtension(filePath) == ".accdb")
        {
            Console.WriteLine("Changed provider to ACE");
            providerName = "Microsoft.ACE.OLEDB.12.0";
        }
        // Establish access to the file
        var accessBuilder = new OleDbConnectionStringBuilder
        {
            Provider = providerName,
            DataSource = filePath
        };

        accessBuilder["Jet OLEDB:Database Password"] = password;

        // Attempt to enter a password and catch if it is incorrect
        using (var conn = new OleDbConnection(accessBuilder.ConnectionString))
        {
            if (ConnectionState.Open != conn.State)
            {
                try
                {
                    // If it fails here, likely due to an actual bad password.
                    conn.Open();
                    Console.WriteLine("0 - Success: Password Correct");
                }
                catch (OleDbException)
                {
                    // Assumed bad password
                    Console.WriteLine("2 - Error: Password Incorrect");
                    return -1;
                }
            }
        }
        return 0;
    }

When I give it an .accdb, the output is:

Changed provider to ACE
Unhandled Exception: System.Data.OleDb.OleDbException: Cannot open database 
''.  It may not be a database that your application recognizes, or the file 
may be corrupt`.

When I give it an .mdb, the output is:

0 - Success: Password Correct

I've tried using a brand new Access file, but it still gives the same error

Joey C
  • 59
  • 2
  • 12
  • 1
    Is the line `accessBuilder["Jet OLEDB:Database Password"] = password;` still valid with `ACE`? – nilsK Jan 04 '19 at 16:34
  • May be worth to look how you're compiling. I'm pretty sure `ACE OLEDB` doesn't have either `x86`, or `x64`, version. Meaning, it either has `x86` version and not `x64` or vise versa – Jaskier Jan 04 '19 at 16:36
  • 1
    Check the value of `accessBuilder.ConnectionString` before using it to connect to the db – OfficialQueensbridgeMurderers Jan 04 '19 at 16:36
  • @nilsK https://www.connectionstrings.com/access-2013/ says Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb; Jet OLEDB:Database Password=MyDbPassword; – Joey C Jan 04 '19 at 16:37
  • @OfficialQueensbridgeMurderers accessBuilder looks good: Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\\Databases\databasePW-purple.accdb; Jet OLEDB:Database Password=purple – Joey C Jan 04 '19 at 16:41
  • @Symon What other provider is a good option of .accdb on a x64 machine? – Joey C Jan 04 '19 at 16:42
  • I'm not 100% if there is one (someone correct me if I'm wrong). [Reference](https://www.connectionstrings.com/access-2007/) – Jaskier Jan 04 '19 at 16:55

1 Answers1

0

When using Ace you need to add a reference to Microsoft ActiveX Data Objects 6.1 Library and possibly Microsoft ActiveX Data Objects Recordset 6.0 Library.

I know this thread is relatively old but I found the solution to this and will post on the other similar answers to this as well for reference.

Clayton.n
  • 3
  • 3