2018年5月14日
C#添加 Sqlite.dll 引用
- 使用nuget,操作最为简单,同时支持32位和64位系统,推荐。缺点是多了 ef、linq等dll文件。
- 手动添加 sqlite.dll。缺点配置繁琐;优点,只需要
SQLite.Interop.dll
和System.Data.SQLite.dll
两个文件
这里只介绍第二种方法,步骤如下:
- 官网 wiki 下载,下载 .NET 2.0 SP2 预编译静态链接库(Precompiled Statically-Linked Binaries for 64-bit Windows (.NET Framework 2.0 SP2))的 binary 版
- 配置 config 文件,具体参考已下载 zip 文件里的 test.exe.config 代码
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.108.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</DbProviderFactories>
</system.data>
异常来自 HRESULT:0x8007000B
System.BadImageFormatException:“试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)”
原因:C# 在调用C++dll时,可能会出现 :试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)这个错误。
实际情况:生成平台目标为AnyCPU(首选32)
解决:首选32 去勾,或者生成平台改为 x64。懒人方法是使用nuget添加 sqlite 引用。