3

I'm working on a C# application that has to read an Access database (.mdb), on Linux. I'm using Mono to compile and run the application.

Suppose I have a test database that I create in Access 2013. It has one table: TestTable, with the default ID column and a testField1 column created with the 'Long Text' type. I insert three rows, with these values for the testField1 column: "foo", "bar", "baz". The database is saved as 'Access 2002-2003 Database (*.mdb)'.

The resulting database (named Test.mdb) is transferred to my Linux box. Just as a sanity check, I can run mdb-export on the database:

$ mdb-export Test.mdb TestTable
ID,testField1
1,"foo"
2,"bar"
3,"baz"

So far, so good. Now, suppose we have a C# program that reads the testField1 column of the table:

using System;
using System.Data.Odbc;

class Program {
    public static void Main(string[] args){
        try {
            OdbcConnection conn = new OdbcConnection("ODBC;Driver=MDBTools;DBQ=/path/to/Test.mdb");
            conn.Open();
            var command = conn.CreateCommand();
            command.CommandText = "SELECT testField1 FROM TestTable";
            var reader = command.ExecuteReader();
            while(reader.Read()){
                Console.WriteLine(reader.GetString(0));
            }
        } catch(Exception e){
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
        }
    }
}

I would expect that running this program would print "foo", "bar", and "baz". However, compiling and running the program does not yield this output:

$ mcs mdb_odbc.cs -r:System.data.dll
$ mono mdb_odbc.exe
潦o


$ # this line added to show the empty lines

My guess is that this is an encoding issue, but I have no idea how to resolve it. Is there a way to fix my program or the environment that it runs in so that the contents of the database are printed correctly? I believe that it is an issue with either ODBC or MDBTools, because in a similar program, a string equality check against fields of a database fails.

I'm using Ubuntu 16.10. mono --version outputs Mono JIT compiler version 5.4.0.167 (tarball Wed Sep 27 18:38:59 EDT 2017) (I built it from source with this patch applied to fix another issue with ODBC). MDBTools, installed through Apt, is version 0.7.1-4build1, and the odbc-mdbtools package is the same version.

I know that the combination of tools and software I'm using is unusual, but unfortunately, I have to use C#, I probably have to use Mono, I have to use an Access database, and I have to use ODBC to access the database. If there's no other way around it, I suppose I could convert the database to another format (SQLite comes to mind).

millinon
  • 1,528
  • 1
  • 20
  • 31
  • I just tried something similar using Python and pyodbc/pypyodbc to see if it might fly, but it couldn't even read the table. Trying to execute `SELECT testField1 FROM TestTable` resulted in the MDBTools ODBC error "TestTable0 does not exist". I'm not too surprised; I have never gotten MDBTools ODBC to work reliably. If you *really* need C#/Mono/.mdb/ODBC then you'll probably have to buy a commercial MS_Access ODBC driver for Linux. Otherwise, you're probably better off to change databases or change platforms. – Gord Thompson Sep 29 '17 at 13:35
  • I tried to reproduce this on Ubuntu 18.04.4. `apt-get install mono-complete` (mono-mcs does not have System.Data.dll). But `ExecuteReader()` throws exception `FATAL UNHANDLED EXCEPTION: System.EntryPointNotFoundException: LocalAlloc` (I removed the try-block). – Martin Thøgersen Apr 14 '20 at 10:15
  • The above exception was caused by an issue https://xamarin.github.io/bugzilla-archives/37/37368/bug.html in older mono versions. I upgraded `mono --version` from 4.6.2 to 6.8.0 as described here: https://linuxize.com/post/how-to-install-mono-on-ubuntu-18-04/ – Martin Thøgersen Apr 14 '20 at 10:40
  • The output `潦o` (foo with fo misread) is confirmed on my end. – Martin Thøgersen Apr 14 '20 at 11:28
  • Thanks for confirming that the issue is reproducible - I think it is probably a bug in MDBTools. I ended up importing the Access database into a SQL Server database on a Windows machine, and then I was able to mount that database on a Linux SQL Server instance, which has a working ODBC driver. – millinon Apr 15 '20 at 15:56

0 Answers0