我收到此错误消息:
“Microsoft.Data.Sqlite.SqliteConnection”的类型初始值设定项引发了异常。
更明确地说,我得到:
Message =“Microsoft.Data.Sqlite.SqliteConnection”的类型初始值设定项引发了异常。
内部异常=系统异常:您需要调用SQLitePCL.raw.SetProvider()。如果您使用的是捆绑包,则可以通过调用SQLitePCL.Batteries.Init()来完成。地址为SQLitePCL.raw.get_Provider(),地址为SQLitePCL. raw. sqlite3_win32_set_directory(Int 32类型,字符串路径),地址为Microsoft。数据.Sqlite.实用程序。BundleInitializer.Initialize(),地址为Microsoft.数据. Sqlite.SqliteConnection.. cctor()
堆栈跟踪=在Microsoft.Data.Sqlite.SqliteConnection.. ctor(字符串连接字符串)在CartographerYou.MainPage.InsertMapRecord(字符串Map名称,字符串Map注解,Int 32首选缩放级别)在CartographerYou.MainPage.btnCre8NewMap_Click(对象发送者,路由事件参数e)
以下是StackTrace中提到的两个方法(一个事件处理程序和一个自定义方法):
private async void btnCre8NewMap_Click(object sender, RoutedEventArgs e)
{
try
{
string mapName = string.Empty;
string mapNotes = string.Empty;
int defaultZoomLevel = 1;
ClearLocations();
// Popul8 the cmbx
for (int i = 1; i < 20; i++)
{
cmbxCre8MapZoomLevels.Items.Add(i.ToString());
}
ContentDialogResult result = await cntDlgCre8Map.ShowAsync();
if (result == ContentDialogResult.Primary)
{
mapName = txtbxMapName.Text;
mapNotes = txtbxMapNotes.Text;
defaultZoomLevel = cmbxCre8MapZoomLevels.SelectedIndex + 1;
// select "Step Into Specific" from context menu
await InsertMapRecord(mapName, mapNotes, defaultZoomLevel);
}
// else do nothing (don't save)
}
catch (Exception ex)
{
string excMsg = string.Format("{0} Inner Exception: {1} Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
MessageDialog exceptionMsgDlg = new MessageDialog(excMsg, "btnCre8NewMap_Click");
await exceptionMsgDlg.ShowAsync();
}
}
private async Task InsertMapRecord(string mapName, string mapNotes, int preferredZoomLevel)
{
path = folder.Path;
connStr = string.Format(connStrBase, path);
try
{
using (SqliteConnection conn = new SqliteConnection(connStr))
{
String query = "INSERT INTO dbo.CartographerMain " +
"(MapName, MapNotes, PreferredZoomLevel) " +
"VALUES (@MapName, @MapNotes, @PreferredZoomLevel)";
using (SqliteCommand cmd = new SqliteCommand(query, conn))
{
cmd.Parameters.AddWithValue("@MapName", mapName);
cmd.Parameters.AddWithValue("@MapNotes", mapNotes);
cmd.Parameters.AddWithValue("@PreferredZoomLevel", preferredZoomLevel);
await conn.OpenAsync();
int result = await cmd.ExecuteNonQueryAsync();
if (result < 0)
{
MessageDialog dialog = new MessageDialog("Error inserting data into CartographerMain");
await dialog.ShowAsync();
}
}
}
}
catch (SqliteException sqlex)
{
string sqlExcMsg = string.Format("{0} Inner Exception: {1} Stack Trace: {2}", sqlex.Message, sqlex.InnerException, sqlex.StackTrace);
MessageDialog dialog = new MessageDialog(sqlExcMsg, "InsertMapRecord");
await dialog.ShowAsync();
}
}
基于需要调用SQLitePCL.raw.SetProvider()的内部异常,我将代码更改为:
using (SqliteConnection conn = new SqliteConnection(connStr))
{
. . .
......改为:
SqliteConnection conn = new SqliteConnection(connStr);
SQLitePCL.raw.SetProvider();
. . .
...但我不知道需要传递给SetProvider()什么-如果这真的是问题的真正解决方案的话。
这是我得到的代码:
需要向SetProvider()传递什么?
2条答案
按热度按时间gcmastyq1#
我认为不鼓励直接使用
SQLitePCL.raw
:......这不是你想要做的。这不是你会用来编写应用程序的SQLite库。它是一个非常薄的C# Package 器,围绕着SQLite的C API。它是“原始的”。
假设您对此没有意见,您可以考虑使用一个 Package 器,例如sqlite-net(作者在他们的页面上提到了这是一个选项):
为了让上面的代码正常工作,我只引用了一个nuget package-它依赖于
SQLitePCLRaw.bundle_green
,我假设它是您无论如何都会安装的包UPD
假设您在使用Microsoft.Data.Sqlite时遇到这个异常,我怀疑这个问题可能源于
SQLitePCLRaw.bundle_e_sqlite3
(底层依赖项)没有为您运行它的环境绑定本机二进制文件。如果您已经安装了
Microsoft.Data.Sqlite
,则应该有e_sqlite3
包作为依赖项,并且可以尝试使用dynamic
提供程序如果不起作用,您可以搜索
SQLitePCLRaw.provider.e_sqlite3.*
作为您的特定目标,并尝试如下所示:wf82jlnq2#
我安装了Microsoft.Data.Sqlite.Core,它“不包括原生SQLite库的副本”。请确保您安装了正确的软件包。如果您还没有SQLite提供程序,请使用Microsoft.Data.Sqlite。