Search code examples
c#ms-accessaccess-violation

AccessViolationException thrown by OleDbConnection.Open()


I am getting a System.AccessViolationException error when trying to use the .Open() method on my OleDbConnection variable. But, confoundingly, it doesn't seem to happen when I run my code on a different computer.

My code is for a service that I want running on a server. It appears to work correctly when I run it on my personal computer, but it throws the AccessViolationException error when I try running it on the server.

Code Versions:

  • I am writing in C# using Visual Studios 2019 on Windows 10

  • OleDbConnection is from "Assembly System.Data, Version=4.0.0.0"

  • ADOX is from "Assembly Interop.ADOX, Version=2.8.0.0", System.Runtime.InteropServices

  • ADODB is from "Assembly Interop.ADODB, Version=2.8.0.0", System.Runtime.InteropServices

My code:

    internal static bool CreateMDB(MySchemaClass schema, string filePath)
    {
        OleDbConnection conn = null;
        bool status = true;
        string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", filePath);

        try
        {
            // Setup new file
            catalog.Create(connectionString);
            ((ADODB.Connection)catalog.ActiveConnection).Close();
            conn = new OleDbConnection(connectionString);
            conn.Open(); // <-- Error occurs here

            // Write to new file
            WriteDataToFile(schema, conn);
        }
        catch (Exception ex)
        {
            status = false;
        }
        finally
        {
            if (conn != null)
                conn.Close();
        }
        return status;
    }

StackTrace:

enter image description here

The best lead I have found so far is this post, but I'm a bit hazy about how to proceed from that starting point.


Solution

  • The underwhelming answer to the problem is that my server was lacking some of the files my personal computer had, and only needed the correct files installed.

    The missing piece in this case was the Microsoft Access Database Engine 2016 Redistributable, which I ended up finding here. Running that executable on my server got the needed files and everything worked after that.