Search code examples
c#ms-accessf#oledb

SEHException on OleDb connection open


I'm developing a small application that will simplify logging, it does so by adding some inputs to an MS Access database through OleDB.

let private conn = new OleDbConnection(connectionString)

let private submitCmd date wins = 
    let cmd = new OleDbCommand("INSERT INTO ArenaStats ([Date], [Wins]) VALUES (@Date, @Wins)", 
                                Connection = conn, CommandType = CommandType.Text)
    ["@Date", box date; "@Wins", box wins]
    |> List.iter (cmd.Parameters.AddWithValue >> ignore)
    cmd


let private submit date wins =
    try
        conn.Open()
        (submitCmd date wins).ExecuteNonQuery() |> ignore
    finally
        conn.Close()

[<CompiledName "AddEntry">]
let addEntry(date:DateTime, wins:int) =
    submit date wins

Now testing this through FSI works just as expected. However, when I consume this API from a C# WPF project it will throw an SEHException at conn.Open(). I am really scratching my head over why this is happening.

Edit

As suggested, I have also tried to implement the same code purely in C# and in the same project, it will throw the same exception at the same place but I am posting the code below for reference.

class MsAccessDatabase : IArenaWinsDatabase {
        private OleDbConnection connection = new OleDbConnection(connectionString);

        private OleDbCommand SubmitCommand(DateTime date, int wins) {
            return new OleDbCommand("INSERT INTO ArenaStats ([Date], [Wins]) VALUES (@Date, @Wins)") {
                Connection = connection,
                CommandType = System.Data.CommandType.Text,
                Parameters = {
                    new OleDbParameter("@Date", date),
                    new OleDbParameter("@Wins", wins)
                }
            };
        }

        public void Submit(DateTime date, int wins) {
            try {
                connection.Open();
                SubmitCommand(date, wins).ExecuteNonQuery();
            }
            finally {
                connection.Close();
            }
        }
    }

Solution

  • With some help from Philip I was able to figure it out. It seems that by default FSI is configured to run in 64-bit by default while the WPF project is set to "prefer 32-bit". Changing the target platform for the WPF project to 64-bit resolved the issue.