首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

解決C#項目在64位系統下SQLite DLL引用問題的實戰經驗

     編輯:Java 相關知識

問題背景

原本在 Windows 7 32位系統上使用 C# 進行專案開發,專案中引用了 32位版本的 System.Data.SQLite.dll。後來重裝系統為 Windows 7 64位版本,發現專案雖能編譯通過,但在運行時拋出異常,無法正常加載 SQLite 組件。

根本原因

64位進程無法加載 32位的非托管 DLL。專案引用的 SQLite 組件標記為 processorArchitecture=x86,導致在 64位環境下運行失敗。

解決方案:修改 .csproj 條件引用

通過編輯專案文件 .csproj,根據編譯平台動態引用對應架構的 DLL。

1. 找到原有引用

<Reference Include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\libs\SQLite\System.Data.SQLite.dll</HintPath>
</Reference>

2. 增加 AnyCPU 條件

<Reference Condition=" '$(Platform)' == 'AnyCPU' " Include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\libs\SQLite\System.Data.SQLite.dll</HintPath>
</Reference>

3. 新增 x64 平台引用

<Reference Condition=" '$(Platform)' == 'x64' " Include="System.Data.SQLite.x64, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\..\libs\SQLite\System.Data.SQLite.x64.dll</HintPath>
</Reference>

配置 Visual Studio 建置平台

  1. 在工具列的「方案平台」下拉選單選擇「Configuration Manager」。
  2. 在「Active solution platform」中新增 x64 平台(保持預設名稱)。
  3. 在專案屬性中,將對應專案的平台設為 x64,其他專案維持 AnyCPU。
  4. 若編譯提示「OutputPath 未設定」,進入專案屬性頁稍微修改一下輸出路徑(改成其他值再改回)即可刷新設定。

注意事項

此方法對於 MSI 安裝包專案可能不完全適用。若涉及安裝部署,建議採用「最小安裝包 + 升級機制」或將編譯產出直接解壓至安裝目錄,避免將整個方案打包成安裝程式。

總結

透過條件引用與平台配置,可讓同一專案在 32/64 位環境下無縫切換,解決 SQLite DLL 架構不匹配的經典問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved