在Visual Studio 2019中使用Mono.Data.Sqlite.dll遇多类错误求助
Hey there, let's break down and fix each of the issues you're encountering one by one—this is a common compatibility hiccup when moving between Mono and Windows .NET environments.
1. Replace Mono.Data.Sqlite with Windows-compatible SQLite Libraries
Mono.Data.Sqlite is built specifically for the Mono runtime, which doesn't play nicely with the Windows .NET Framework/.NET Core runtime used by Visual Studio. The fix here is to switch to official, Windows-supported SQLite packages:
- For .NET Framework projects: Install the
System.Data.SQLiteNuGet package. This is the mature, official SQLite provider for classic .NET Framework. - For .NET Core/.NET 5+ projects: Install
Microsoft.Data.Sqlite(the modern, cross-platform alternative maintained by Microsoft).
Steps to swap the library:
- Right-click your project in VS2019 → Manage NuGet Packages.
- Remove any existing references to
Mono.Data.Sqlitefirst. - Search for the appropriate package (System.Data.SQLite or Microsoft.Data.Sqlite) and install it.
- Update your code's using statements:
- Replace
using Mono.Data.Sqlite;withusing System.Data.SQLite;(for System.Data.SQLite) - Or
using Microsoft.Data.Sqlite;(for Microsoft.Data.Sqlite)
- Replace
Most SQLite operations (like connections, commands, readers) have nearly identical APIs, so you won't need to rewrite most of your code—just adjust the namespace and class name casing (e.g., SQLiteConnection instead of Mono's SqliteConnection).
2. Resolve System.BadImageFormatException (x86 Compilation Mode)
This error happens when the native SQLite binary (sqlite3.dll) doesn't match your project's target architecture. Since you're using x86 mode, here's how to fix it:
- Confirm target architecture: Right-click your project → Properties → Build → Ensure Platform target is set to x86.
- Verify native DLL copy: The NuGet package should automatically copy the correct x86 sqlite3.dll to your output directory, but if it doesn't:
- Navigate to your NuGet packages folder (usually
[Project Root]/packages). - Find the System.Data.SQLite.Core package folder, then go to
runtimes/win-x86/native/to get the x86 sqlite3.dll. - Copy this DLL to your project's output folder (e.g.,
bin/Debug/x86).
- Navigate to your NuGet packages folder (usually
- Enable "Copy Local": Right-click the SQLite reference in your project → Properties → Set Copy Local to
True—this ensures the DLL is always copied to the output directory during compilation.
3. Clean Up Mono.Data.Sqlite Residues
Old references or cached files can cause lingering issues:
- Clean the solution: Go to Build → Clean Solution to remove old compiled files.
- Delete package cache: If you're using packages.config, delete the
packagesfolder in your project root, then right-click the project → Restore NuGet Packages to get fresh copies. - Check the project file: Open your .csproj file in a text editor and remove any leftover
<Reference Include="Mono.Data.Sqlite..." />entries if they exist.
After following these steps, rebuild your project and run it—this should resolve all the errors you're seeing.
内容的提问来源于stack exchange,提问作者Davide Parisi

