Visual Studio 2017 UWP项目中无法添加Microsoft.Data.Sqlite引用的解决方法
Hey there, let's tackle this issue step by step—since you're working with UWP in VS2017, there are a few specific checks and fixes to get Microsoft.Data.Sqlite working properly.
First: Clean Up Conflicting Packages
You mentioned installing multiple SQLite-related packages, and some of them might be incompatible with UWP:
- Uninstall
System.Data.SQLiteimmediately. This package is built for traditional .NET Framework apps, not UWP, and it will clash withMicrosoft.Data.Sqlite. - Keep
Microsoft.Data.Sqlite,sqlite-net, andsqlite-net-pcl—these are compatible with UWP, but make sure you're using versions that work with VS2017's supported frameworks. ForMicrosoft.Data.Sqlite, stick to versions 3.1.x or earlier (newer versions require .NET Core 3.1+, which isn't fully supported in VS2017 for UWP).
Verify Project Target Version
UWP requires a minimum Windows 10 version to support Microsoft.Data.Sqlite:
- Right-click your UWP project → Properties
- Go to the Application tab
- Ensure your Target version is at least Windows 10 Anniversary Update (10.0; Build 14393) and your Minimum version is no lower than that (or a compatible older version like 10586, but 14393 is safer).
Fix NuGet Installation & Cache Issues
Sometimes NuGet doesn't properly add references even after installation. Try these steps:
- Right-click your project → Clean
- Right-click your project → Rebuild
- Close Visual Studio entirely, then delete the
binandobjfolders in your project directory - Reopen VS, right-click your project → Manage NuGet Packages, and re-install
Microsoft.Data.Sqlite(make sure to pick the compatible version mentioned earlier)
Add the Using Statement & Test with Valid Code
Once the above steps are done, you should be able to add the using statement in MainPage.xaml.cs. Also, remember that UWP restricts file access—you can't use arbitrary file paths for your SQLite database. Use the app's local storage instead:
using Microsoft.Data.Sqlite; using Windows.Storage; using System.IO; private void LoadDatabaseData() { // Get the path to your database in the app's local folder string dbFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "your_database_name.db"); // Create and open the connection using (SqliteConnection connection = new SqliteConnection($"Data Source={dbFilePath}")) { connection.Open(); // Example query SqliteCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM YourTableName"; using (SqliteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { // Read data from the reader (adjust based on your table schema) int id = reader.GetInt32(0); string name = reader.GetString(1); // Do something with the data, like update UI elements } } } }
Final Checks
- Double-check that
Microsoft.Data.Sqliteappears under References in your Solution Explorer (expand the project node to see this) - If you still get errors, try updating your NuGet Package Manager in VS2017 (go to Tools → Extensions and Updates to check for updates)
内容的提问来源于stack exchange,提问作者CorentinMerit

