原本在 Windows 7 32位系統上使用 C# 進行專案開發,專案中引用了 32位版本的 System.Data.SQLite.dll。後來重裝系統為 Windows 7 64位版本,發現專案雖能編譯通過,但在運行時拋出異常,無法正常加載 SQLite 組件。
64位進程無法加載 32位的非托管 DLL。專案引用的 SQLite 組件標記為 processorArchitecture=x86,導致在 64位環境下運行失敗。
通過編輯專案文件 .csproj,根據編譯平台動態引用對應架構的 DLL。
<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>
<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>
<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>
x64 平台(保持預設名稱)。x64,其他專案維持 AnyCPU。此方法對於 MSI 安裝包專案可能不完全適用。若涉及安裝部署,建議採用「最小安裝包 + 升級機制」或將編譯產出直接解壓至安裝目錄,避免將整個方案打包成安裝程式。
透過條件引用與平台配置,可讓同一專案在 32/64 位環境下無縫切換,解決 SQLite DLL 架構不匹配的經典問題。