问题
I am getting the following error when I run the Mobile Emulator while trying to compile and run a VS 2015 UWP app. The app runs fine when using the Local Machine or Simulator.
System.TypeInitializationException was unhandled by user code
HResult=-2146233036
Message=The type initializer for 'SQLitePCL.raw' threw an exception.
Source=SQLitePCL.raw
TypeName=SQLitePCL.raw
StackTrace:
at SQLitePCL.raw.sqlite3_open_v2(String filename, sqlite3& db, Int32 flags, String vfs)
at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks)
at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks)
at App3.MainPage.LoadSQLData()
at App3.MainPage..ctor()
at App3.App3_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_MainPage()
at App3.App3_XamlTypeInfo.XamlUserType.ActivateInstance()
InnerException:
HResult=-2146233052
Message=Unable to load DLL 'sqlite3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Source=SQLitePCL.raw
TypeName=""
StackTrace:
at SQLitePCL.SQLite3Provider.NativeMethods.sqlite3_win32_set_directory(UInt32 directoryType, String directoryPath)
at SQLitePCL.SQLite3Provider..ctor()
at SQLitePCL.raw..cctor()
I have the following references: SQLite for Universal Windows 3.9.1 sqlite-net-pcl Microsoft Visual C++ 2013 Runtime Package for Universal Windows
The code where the error occurs in the "using (var db......:
int recCtr = 0;
var root = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "BaseBallOU.db");
List<string> NHLCollection = new List<string>();
using (var db = new SQLite.SQLiteConnection(dbPath))
{
var NHLlist = db.Table<Teams>().ToList();
foreach (var item in NHLlist)
{
recCtr++;
NHLCollection.Add(item.TeamName.ToString());
}
}
I see a number of similar posts, but some are dated and not using the most recent SQLite libs and pcls.
I'm looking for the correct SQLite dlls, sqlite pcls, runtimes (e.g. VC++ 2013? And version numbers that can be used to compile and run a UWP app on both the local machine and phone emulator.
TIA
回答1:
Following is how I work with SQLite:
Download and install Sqlite visual studio extension from http://sqlite.org/download.html
Create a new blank c# Universal Windows Platform app.
Right click on the References of project -> Add Reference -> Universal Windows -> Extension -> add SQLite for Universal App Platform and its dependency Visual C++ 2015 Runtime for Universal Windows
Right click on the project node -> Manage NuGet packages -> Search SQLite.Net-PCL -> Install the default version 3.0.5
Change your code to the following:
using (var db = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), dbPath)) { ... }
回答2:
Step 1: Install SQLite VSIX pacakge for Universal Windows Platform development using Visual Studio 2015
https://www.sqlite.org/2016/sqlite-uwp-3130000.vsix
Step 2: Install NuGet package SQLite.NET-PCL
PM> Install-Package SQLite.NET-PCL
Step 3: Add references
Add Reference -> Universal Windows ->Extensions -> Make sure the following packages have been checked: "SQLite for Universal Windows Platform" "Visual C++ 2015 Runtime for Universal Windows Platform Apps"
If you didn't add reference for VC++ 2015 Runtime, you will get an error mentioned that "Unable to load DLL 'sqlite3' in SQLite Net Platform WinRT" when creating DB connection.
来源:https://stackoverflow.com/questions/33320191/sqlite-uwp-error-with-mobile-emulator-windows-10