.net 构造 System.Data.SQLite.SQLiteConnection 时是什么导致 System.BadImageFormatException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2048914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:47:06  来源:igfitidea点击:

What causes System.BadImageFormatException when constructing System.Data.SQLite.SQLiteConnection

.netsqlite32bit-64bitsystem.data.sqlite

提问by Tim Murphy

I've broken the code down to the smallest possible statement:

我已将代码分解为尽可能小的语句:

Dim cn As System.Data.SQLite.SQLiteConnection

And I get the following error when calling the code from a WinForm applicaiton:

从 WinForm 应用程序调用代码时出现以下错误:

System.BadImageFormatException: Could not load file or assembly 'System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format. File name: 'System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139'

System.BadImageFormatException:无法加载文件或程序集“System.Data.SQLite,版本=1.0.65.0,Culture=neutral,PublicKeyToken=db937bc2d44ff139”或其依赖项之一。试图加载格式不正确的程序。文件名:'System.Data.SQLite,版本=1.0.65.0,文化=中性,PublicKeyToken=db937bc2d44ff139'

Yet calling the same piece of code from MS Unit Test I do not get the error, plus the full code set works as expected.

然而,从 MS 单元测试调用同一段代码我没有得到错误,加上完整的代码集按预期工作。

回答by Hans Passant

SqlLite contains unmanaged code, you can't run it on a 64-bit operating system unless you deploy the 64-bit version. Quick fix: Project + Properties, Build tab, Platform Target = x86.

SqlLite 包含非托管代码,您不能在 64 位操作系统上运行它,除非您部署 64 位版本。快速修复:项目 + 属性,构建选项卡,平台目标 = x86。

回答by Anton Gogolev

Make sure you use correct assembly with respect to 32 and 64 bits.

确保针对 32 位和 64 位使用正确的程序集。

回答by Denis

I solved the problem by downloading a new version of the SQLite DLL via NuGet: http://gurustop.net/blog/2011/05/19/sqlite-database-nuget-package-common-problems-solved/

我通过 NuGet 下载新版本的 SQLite DLL 解决了这个问题:http: //gurustop.net/blog/2011/05/19/sqlite-database-nuget-package-common-problems-solved/