解決Sqlite“無法加載?DLL“e_sqlite3”:?找不到指定的模塊”的方法
今天在使用 NuGet 安裝 System.Data.SQLite 庫后,發(fā)現(xiàn)運行就報錯:System.DllNotFoundException:“無法加載 DLL“e_sqlite3”: 找不到指定的模塊。 (異常來自 HRESULT:0x8007007E)。”
百度查了半天,啥都沒有,我也是醉了,問AI也沒問出個答案,回答全是亂七八糟的,于是我又查看了 System.Data.SQLite 的自述文件和包詳細信息,是這樣:


啥都沒寫,這種庫也是奇葩,我有點好奇能用在商業(yè)項目中么,一點規(guī)范都沒有,有點像個人開發(fā)者的行為。
我又搜了下 e_sqlite3,不是沒用就是要錢,what ??
后面我又在 百度 找 System.Data.SQLite 官網(wǎng),找到了這么一個地址

打開一看,剛好找到了答案,界面如下:

這里的描述文字寫著:
System.Data.SQLite is available on nuget.org. Typical usage would involve adding two packages, one for System.Data.SQLite, and one containing a native built of SQLite. For example:
翻譯成中文:
System.Data.SQLite 可在 nuget.org 上獲取。典型用法通常需要添加兩個 NuGet 包:一個用于 System.Data.SQLite,另一個包含 SQLite 的本機構(gòu)建(例如 System.Data.SQLite.Core)。例如:
看到這里我算是知道了,還需要安裝另一個庫:SourceGear.sqlite3
這下就好辦了,于是我就裝了 SourceGear.sqlite3 這個庫,如下:

SourceGear.sqlite3 這個庫 包詳細信息這里有一段描述
This package contains builds of the native SQLite code for various platforms.
These builds have "e_sqlite3" as the base name. The first three numbers in the version number of this package indicate the version of SQLite that was used to build it. The fourth number, if there is one, is incremented as needed for packaging-specific changes.
翻譯成中文:
此軟件包包含適用于不同平臺的原生 SQLite 代碼構(gòu)建版本。這些構(gòu)建版本的基礎(chǔ)名稱均為 ??"e_sqlite3"??。軟件包版本號中的前三位數(shù)字表示構(gòu)建時所使用的 SQLite 版本,若存在第四位數(shù)字,則會根據(jù)打包相關(guān)的修改需求進行遞增
好吧,我先搭建下 Sqlite 的環(huán)境,讓 Winform 能調(diào)用 Sqlite 進行增刪改查,項目我就不上傳了,直接貼一個 SqliteHelper.cs 好了
using System;
using System.Data;
using System.Data.SQLite;
using System.Collections.Generic;
using System.Reflection;
public class SqliteHelper
{
/// <summary>
/// 數(shù)據(jù)庫連接字符串
/// </summary>
public static string ConnectionString { get; set; } = "Data Source=work.db;Version=3;Pooling=True;Journal Mode=WAL;Cache=Shared;Synchronous=NORMAL;BusyTimeout=5000;";
/// <summary>
/// 執(zhí)行 插入、更新、刪除 操作
/// </summary>
/// <param name="sql">要執(zhí)行的SQL語句(INSERT/UPDATE/DELETE)</param>
/// <param name="parameters">SQL參數(shù)數(shù)組,防止SQL注入</param>
/// <returns>p1.是否成功,p2.錯誤信息</returns>
public static (bool, string) ExecuteSql(string sql, params SQLiteParameter[] parameters)
{
if (ConnectionString == null)
return (false, "請先設(shè)置 ConnectionString");
try
{
int line = 0;
using (var conn = new SQLiteConnection(ConnectionString))
{
conn.Open();
using (var cmd = new SQLiteCommand(sql, conn))
{
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(parameters);
// ExecuteNonQuery 返回受影響的行數(shù)
line = cmd.ExecuteNonQuery();
}
conn.Close();
}
return (true, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return (false, ex.Message);
}
}
/// <summary>
/// 執(zhí)行查詢SQL
/// </summary>
/// <param name="sql">SQL語句</param>
/// <param name="parameters">SQL參數(shù)數(shù)組,防止SQL注入</param>
/// <returns>p1.是否成功,p2.查詢的數(shù)據(jù),p3.錯誤信息</returns>
public static (bool, DataTable, string) Query(string sql, params SQLiteParameter[] parameters)
{
if (ConnectionString == null)
return (false, null, "請先設(shè)置 ConnectionString");
try
{
var dataTable = new DataTable();
using (var conn = new SQLiteConnection(ConnectionString))
{
conn.Open();
using (var cmd = new SQLiteCommand(sql, conn))
{
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(parameters);
//使用數(shù)據(jù)適配器填充 DataTable
using (var adapter = new SQLiteDataAdapter(cmd))
adapter.Fill(dataTable);
}
conn.Close();
}
return (true, dataTable, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return (false, null, ex.Message);
}
}
/// <summary>
/// 執(zhí)行查詢SQL
/// </summary>
/// <typeparam name="T">結(jié)果集列表中對象的類型,需提供無參構(gòu)造函數(shù)</typeparam>
/// <param name="sql">SQL語句</param>
/// <param name="parameters">SQL參數(shù)數(shù)組,防止SQL注入</param>
/// <returns>p1.是否成功,p2.查詢的數(shù)據(jù),p3.錯誤信息</returns>
public static (bool, List<T>, string) Query<T>(string sql, params SQLiteParameter[] parameters) where T : new()
{
if (ConnectionString == null)
return (false, null, "請先設(shè)置 ConnectionString");
List<T> list = new List<T>();
try
{
using (var conn = new SQLiteConnection(ConnectionString))
{
conn.Open();
using (var cmd = new SQLiteCommand(sql, conn))
{
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(parameters);
using (var reader = cmd.ExecuteReader())
{
// 獲取一次性獲取列的元數(shù)據(jù),用于反射匹配屬性
var schemaTable = reader.GetSchemaTable();
// 建立列名到序號的映射
var ordinals = new Dictionary<string, int>();
foreach (DataRow col in schemaTable.Rows)
{
string colName = col["ColumnName"].ToString();
ordinals[colName] = (int)col["ColumnOrdinal"];
}
// 獲取泛型類型的所有可寫屬性
Type objType = typeof(T);
PropertyInfo[] properties = objType.GetProperties();
while (reader.Read())
{
T obj = new T();
foreach (PropertyInfo prop in properties)
{
if (!prop.CanWrite) continue;
if (ordinals.ContainsKey(prop.Name))
{
object value = reader.GetValue(ordinals[prop.Name]);
if (value != DBNull.Value)
{
// 轉(zhuǎn)換為屬性的實際類型后賦值
Type targetType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
object safeValue = Convert.ChangeType(value, targetType);
prop.SetValue(obj, safeValue);
}
}
}
list.Add(obj);
}
reader.Close();
}
}
conn.Close();
}
return (true, list, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return (false, null, ex.Message);
}
}
}
運行項目,你會發(fā)現(xiàn)項目依然報錯:

翻譯成中文:此軟件包不支持 Any CPU 版本
也就是說,生成的時候,不能使用 Any CPU
點擊項目的 配置管理器

在 “活動解決方案平臺” 這里展開下拉框,點擊新建

選擇一個平臺,復(fù)制設(shè)置我選擇空(最好不要和我圖片一樣,我這里是測試項目),然后點擊確定。關(guān)閉這個窗體。

然后在 運行這里就能看到 x64 的選項了

選擇 x64,現(xiàn)在 Sqite 能正常的運行了
如果你不想使用 x64 ,想刪除這個選項,也很簡單,同樣是點擊配置管理器
找到活動解決方案平臺

點擊編輯

然后選中 x64,然后點擊移除就行了
end
到此這篇關(guān)于解決Sqlite“無法加載 DLL“e_sqlite3”: 找不到指定的模塊”的文章就介紹到這了,更多相關(guān)Sqlite 無法加載 DLL“e_sqlite3”: 找不到指定的模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
sqlite循環(huán)批量插入數(shù)據(jù)采用批處理文件實現(xiàn)
需要在sqlite數(shù)據(jù)庫中插入大量測試數(shù)據(jù),需要通過一個批處理文件來循環(huán)調(diào)用插入sqlite語句,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04
SQLite3 在嵌入式C環(huán)境中存儲音頻/視頻文件的最優(yōu)方案
本文探討了SQLite3在嵌入式C環(huán)境中存儲音視頻文件的優(yōu)化方案,推薦采用文件路徑存儲結(jié)合元數(shù)據(jù)管理,兼顧效率與資源限制,小文件可使用BLOB存儲,輔以壓縮和故障安全機制,適合資源受限的嵌入式系統(tǒng),對SQLite3存儲文件相關(guān)知識感興趣的朋友一起看看吧2025-06-06

