If you *must* use Microsoft Access on .NET on Linux
At $DAYJOB, I was once tasked with migrating an old codebase that was written on .NET Framework to a modern ASP.NET Core application. One of the features we had to preserve was the ability to generate Access database (.mdb) export files. The old app was deployed on a Windows machine; but this new one had to be deployed on Linux. This was where I hit a roadblock: I found that the OleDb driver (the driver used to connect to Access databases) cannot run on Linux because it needs a Windows COM component.

After doing some research online, there are a few options that people suggested:
- Migrate to other database (pgsql, mysql etc,)
- Use mdb-tools
- Deploy a thin Windows container app whose only purpose is to export the Access database. The .NET app will trigger it via an endpoint or command.
- Use UCanAccess, which is a Java library that can read/write Access database files.
I tried the options in order from top to bottom.
- What? Nope! I must use Access.
- I tested it on Linux and Windows. It can read from Access, but not write to it (bruh..)
- Well, this might work, but it adds complexity because I would need to maintain different apps and rewrite the component from C# code in Java.
- This works! That’s why I’m writing this whole blog post. Read along.
I have actually used UCanAccess previously, but not directly. UCanAccess is included in DBeaver, the database management tool that I use. So, before going any further, I tested the driver’s read and write capabilities on both Windows and WSL. It works like a charm! Now, I just need to integrate it with my .NET app.
UCanAccess is a Java library, so how do we even use it in .NET? Meet IKVM, a tool that allows you to translate a JAR file into a .NET assembly (.dll).
Prepare UCanAccess library
First, you’ll need the UCanAccess JAR files. You can get them from the UCanAccess download page (download the *bin.zip, not *-src.zip). The latest version at the time of writing is 5.0.1. Read the steps below:
-
Extract the archive.
-
Merge all the JAR files into one fat JAR file. Run
jarin your terminal to check whether you have the JDK installed. If not, run the command below to install it.Debian/Ubuntu:
Terminal window sudo apt install default-jdkmacOS:
Terminal window brew install javaecho 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrcsource ~/.zshrcThen,
cdinto theUCanAccess-5.0.1.binfolder and run the command below to create a ‘fat’ JAR file.Terminal window mkdir merged && cd merged && jar xf ../ucanaccess-5.0.1.jar && jar xf ../loader/ucanload.jar && for j in ../lib/*.jar; do jar xf "$j"; done && jar cf ../ucanaccess-all.jar . && cd .. && rm -rf mergedThis command will create a new file called
ucanaccess-all.jarin the same folder. This file contains all the classes from the original UCanAccess Jar files.
UCanAccess Jar files
Setup on .NET project
Now, on the .NET side, first install IKVM from NuGet. Put the file ucanaccess-all.jar somewhere in your project folder, then reference it in your .csproj file as shown below:
<ItemGroup> <IkvmReference Include="UCanAccessBin\UCanAccess-5.0.1.bin\ucanaccess-all.jar"> <AssemblyName>ucanaccess</AssemblyName> <AssemblyVersion>5.0.1.0</AssemblyVersion> <AssemblyFileVersion>5.0.1.0</AssemblyFileVersion> </IkvmReference></ItemGroup>When you build the project, IKVM will convert the JAR file into a ucanaccess.dll file in the output folder. You can then reference this DLL in your program.
Here is a basic example of how to connect to the database:
// load ucanaccess dllikvm.runtime.Startup.addBootClassPathAssembly(System.Reflection.Assembly.Load("ucanaccess"));
// Load driverjava.lang.Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
// build the connection stringvar accessPath = "/Users/fareez/Downloads/Access_sample.mdb";var connectionString = $"jdbc:ucanaccess://{accessPath}";var connection = java.sql.DriverManager.getConnection(connectionString);
// do something with the connectionvar statement = connection.createStatement();Peculiarities
When testing it more thoroughly, we found some issues with SQL Insert statements. Consider the following SQL statement. Ignore the fact that a name wouldn’t be in square brackets etc, this is for demo purposes only:
INSERT INTO [Contacts] (ID, [First Name], [Last Name]) VALUES (1, 'Test 1', 'My text has [square bracket]. Hi.')And another one:
INSERT INTO [Contacts] (ID, [First Name], [Last Name]) VALUES (2, 'Test 2', 'SomeText; Union; World')Pay attention to the Last Name column. The first statement has a square bracket in the value, while the second statement has a semicolon in the value. When we run these statements, we get different results:
My text has (square bracket)Hi.SomeText Union; WorldSpot the differences? The first statement has the square bracket transformed into a parenthesis, while the second statement has the semicolon removed.
This is because UCanAccess uses HSQLDB as an intermediate database before writing to the Access database. And the parser or whatever mechanism to read the query might have trouble parsing data with SQL syntax; in this case a square bracket or the word ‘UNION’. Now, What can you do to work around this?
In ucanaccess on .NET/JAVA, you can use a PreparedStatement instead of a Statement. This will allow you to use parameters in your SQL statement, which will be properly escaped and handled by the driver. Given below are examples of both scenarios.
// Don't do this.var randomNum = new Random().Next(30, 50);var badInsertStatement = $"INSERT INTO [Contacts] (ID, [First Name], [Last Name]) VALUES ({randomNum}, 'Test', 'My text has [square bracket]. Hi.')";using var createStatement = connection.createStatement();createStatement.execute(badInsertStatement);
// Do this instead.var goodInsertStatement = "INSERT INTO [Contacts] (ID, [First Name], [Last Name]) VALUES (?, ?, ?)";using (var goodStatement = connection.prepareStatement(goodInsertStatement)){ goodStatement.setInt(1, ++randomNum); goodStatement.setString(2, "Test"); goodStatement.setString(3, "My text has [square bracket]. Hi."); goodStatement.executeUpdate();}That’s all. Please see this repo for a full code example, instructions, and results. It’s a bit funny to see Java code inside a .NET project, but this is the only way to interface with an Access database within a .NET project. I hope this can help you if you are in the same situation as me.