.NET Framework 4.8 + Microsoft.Data.Sqlite 报 Library e_sqlite3 not found
具体移步:我的博客
提出问题
最近在 .NET Framework 4.8 下使用 Microsoft.Data.Sqlite 时,运行程序报错:
System.Exception: "Library e_sqlite3 not found"
即便安装了 Microsoft.Data.Sqlite 和 SQLitePCLRaw.bundle_e_sqlite3,程序仍找不到 DLL。下面是完整排查和解决方案。
问题原因
- native DLL 没部署到输出目录 SQLitePCLRaw.bundle_e_sqlite3 只是包装
- 真正提供 DLL 的是 SQLitePCLRaw.lib.e_sqlite3
- 传统 .NET Framework 项目不会自动把 DLL 复制到:
bin\x64\Debug\runtimes\win-x64/native/e_sqlite3.dll - 平台不匹配,DLL 是 64 位,项目必须 Platform target = x64
- 缺少 VC++ 运行时:e_sqlite3.dll 依赖 Microsoft Visual C++ Redistributable x64
NuGet 配置
在 packages.config 中保留关键包:
<package id="Microsoft.Data.Sqlite" version="6.0.15" targetFramework="net48" />
<package id="SQLitePCLRaw.bundle_e_sqlite3" version="2.1.10" targetFramework="net48" />
<package id="SQLitePCLRaw.lib.e_sqlite3" version="2.1.10" targetFramework="net48" />
⚠️ 注意:不要混用多个不同版本的 Microsoft.Data.Sqlite,否则可能导致 native DLL 部署失败。
初始化代码
在 Program.cs 的入口 Main() 中最开始调用:
csharp
using SQLitePCL;
class Program
{
static void Main(string[] args)
{
Batteries_V2.Init();
// 你的 Sqlite 相关代码
}
}
解决步骤(手动拷贝 DLL)
1、找到 e_sqlite3.dll,路径通常在: packages\SQLitePCLRaw.lib.e_sqlite3.2.1.10\runtimes\winx64\native\e_sqlite3.dll
2、拷贝到你的项目输出目录:
Simulated_LoginRegi\bin\x64\Debug
或者按照报错路径创建:
Simulated_LoginRegi\bin\x64\Debug\runtimes\win-x64\native\并放入 e_sqlite3.dll。
3、确认 Platform target = x64
4、安装 VC++ Redistributable x64
✅ 这样程序就可以正常运行。
项目目录结构示例
Simulated_LoginRegi/
├─ packages/
│ └─ SQLitePCLRaw.lib.e_sqlite3.2.1.10/
│ └─ runtimes/win-x64/native/e_sqlite3.dll
├─ bin/
│ └─ x64/Debug/
│ ├─ Simulated_LoginRegi.exe
│ └─ e_sqlite3.dll <-- 手动拷贝的位置
├─ Program.cs
├─ Simulated_LoginRegi.csproj
└─ packages.config
总结
根因:.NET Framework 4.8 传统项目不会自动复制 e_sqlite3.dll
解决方法:手动拷贝 DLL + 调用 Batteries_V2.Init() + x64 平台 + 安装 VC++ Redistributable
这是在 .NET Framework 4.8 下使用 Microsoft.Data.Sqlite 的稳定方案