简体   繁体   English

OleDb 连接打开时的 SEHException

[英]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.我正在开发一个可以简化日志记录的小应用程序,它通过 OleDB 向 MS Access 数据库添加一些输入来实现。

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.现在通过 FSI 进行测试可以正常工作。 However, when I consume this API from a C# WPF project it will throw an SEHException at conn.Open() .但是,当我从 C# WPF 项目使用这个 API 时,它会在conn.Open()处抛出一个SEHException 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.正如所建议的,我也尝试过完全在 C# 和同一个项目中实现相同的代码,它会在同一个地方抛出相同的异常,但我在下面发布代码以供参考。

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();
            }
        }
    }

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".似乎默认情况下,FSI 配置为默认以 64 位运行,而 WPF 项目设置为“首选 32 位”。 Changing the target platform for the WPF project to 64-bit resolved the issue.将 WPF 项目的目标平台更改为 64 位解决了该问题。

When trying to run the following code:尝试运行以下代码时:

var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", FilePath);
OleDbConnection OleDbConnection = new System.Data.OleDb.OleDbConnection(connectionString);
OleDbConnection.Open();

An SEHException exception is thrown at runtime , with the error message 'External Component has thrown an Exception'运行时抛出 SEHException 异常,并显示错误消息“外部组件抛出异常”

This will usually occur when the build configuration platform in Visual Studio is incorrect, this can occur in both build configuration platforms, x86 and x64.当 Visual Studio 中的构建配置平台不正确时,通常会发生这种情况,这可能发生在 x86 和 x64 两种构建配置平台中。

This is due to a mismatch between the build configuration platform of your project and the Microsoft Access Database Engine which is installed on your machine.这是由于项目的构建配置平台与安装在计算机上的 Microsoft Access 数据库引擎不匹配

In order to resolve this error:为了解决这个错误:

  • Change the build configuration platform in Visual Studio - make sure it matches the Microsoft Access Database Engine version on your machine在 Visual Studio 中更改构建配置平台 - 确保它与您计算机上的 Microsoft Access 数据库引擎版本匹配
  • Recompile and run your project重新编译并运行你的项目
  • The run time error should now be resolved现在应该解决运行时错误

I know this is a really old thread, but I just ran into the same problem with a different solution.我知道这是一个非常古老的线程,但我只是遇到了不同的解决方案的相同问题。

When I installed the Access Database Engine 2016 Redistributable (x64) for the ACE provider the installer gave me an error that VCRUNTIME140.DLL wasn't installed, but the installer completed anyways and the ACE provider was available.当我为 ACE 提供程序安装 Access Database Engine 2016 Redistributable (x64) 时,安装程​​序给了我一个错误,指出未安装 VCRUNTIME140.DLL,但安装程序仍然完成并且 ACE 提供程序可用。

Uninstalling the Access Database Engine, installing the VC++ 2015 Redistributable R3 ( https://www.microsoft.com/en-us/download/confirmation.aspx?id=52685 ), then re-installing the Access Database Engine solved the problem.卸载 Access 数据库引擎,安装 VC++ 2015 Redistributable R3 ( https://www.microsoft.com/en-us/download/confirmation.aspx?id=52685 ),然后重新安装 Access 数据库引擎解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM