【问题标题】:C# SQLite Win 8.1 System.BadImageFormatExceptionC# SQLite Win 8.1 System.BadImageFormatException
【发布时间】:2014-07-04 02:07:32
【问题描述】:

我正在尝试在 Visual Studio 2013 上的 WinRT 8.1 上使用 SQLite 进行编程。我使用 WinRT 8.1 VSIX 安装了 SQLite,并为 Windows Phone 8.1 安装了相同的 SQLite,还从 nuget 加载了sqlite-net

我用得到以下错误:

An exception of type 'System.BadImageFormatException' occurred in TimeTableAPP.Windows.exe but was not handled in user code

从这两行代码:

SQLiteAsyncConnection cneu = new SQLiteAsyncConnection("newDB.db");
await cneu.CreateTableAsync<TestTable>();

相同
var conn = new SQLiteAsyncConnection(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "newDB.db"));
await cneu.CreateTableAsync<TestTable>();

使用测试表

[Table("TestTable")]
public class TestTable
{
    [PrimaryKey, AutoIncrement]
    public long Id { get; set; }
    public string nomnomnom { get; set; }
}

这个问题似乎经常发生,通常通过将平台目标设置为 x86 来解决。如您所见,我这样做了:

我不知道该尝试什么了。我对 VC2013 安装进行了修复,卸载并重新安装了 VSIX,并为共享的 Windows 8.1 和 Windows Phone 8.1 创建了一个项目,Windows Phone 没有触发错误,但 Windows Store App 使用相同的代码触发了错误。

【问题讨论】:

  • 你能用这个代替吗? nuget.org/packages/System.Data.SQLite
  • 我不习惯nuget,当我复制粘贴命令时,它给了我一个成功的回报:安装'System.Data.SQLite 1.0.93.0'。成功安装“System.Data.SQLite 1.0.93.0”。将“System.Data.SQLite 1.0.93.0”添加到 App2。成功将“System.Data.SQLite 1.0.93.0”添加到 App2。但我不能像以前那样添加using System.Data.SQLite,也不能添加using SQLite

标签: c# sqlite windows-runtime visual-studio-2013


【解决方案1】:

根据MSDN,此错误是由于库或程序的格式错误(通常不匹配)而发生的 - 如果您的目标是 x86,则可能会发生这种情况,但如果您的目标是 x64 或有超出您自己的代码的不匹配。如果您的程序不是 x86,简单地设置为 x86 并不能解决它。确保您尝试使用 x64 和 Any CPU,并查看有关 SQLite 运行的任何可用文档。

另外不要忘记,您可以尝试直接安装库而不是从 NuGet 安装,因为库本身可能存在一些冲突。不在你的控制范围内。

【讨论】:

  • 不可能有不匹配,因为我创建了一个新项目,其中仅显示了上面显示的行。正如您在我的配置文件中看到的,我没有将目标设置为 x64。将其更改为 any cpu 并不是诀窍,因为您必须指定架构,使用目标 x64 编译只会触发 An exception of type 'SQLite.SQLiteException' occurred in App2.exe but was not handled in user code
  • 直接安装库是什么意思?我不能使用来自 sqlite.org 的 dll,因为它说 The project targets '.NETCore' while the file reference targets '.NETFramework'. This is not a supported scenario.
  • 实际上,x64 正在修复(部分)问题。如果你得到一个 SQLite 异常,这意味着它在完全加载 DLL 失败之前输入了代码。至于使用'.NETCore'可能需要自己修复项目文件
  • SQLite 的新更新解决了这个问题,看来这是一个已知问题。
猜你喜欢
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多