【发布时间】:2021-03-27 05:19:22
【问题描述】:
我收到此错误消息:
“Microsoft.Data.Sqlite.SqliteConnection”的类型初始化器 抛出异常。
更明确地说,我得到:
Message = 的类型初始化器 'Microsoft.Data.Sqlite.SqliteConnection' 抛出异常。
Inner Exception = System.Exception: 你需要调用 SQLitePCL.raw.SetProvider()。如果您使用的是捆绑包,这 通过调用 SQLitePCL.Batteries.Init() 来完成。在 SQLitePCL.raw.get_Provider() 在 SQLitePCL.raw.sqlite3_win32_set_directory(Int32 typ,字符串路径)在 Microsoft.Data.Sqlite.Utilities.BundleInitializer.Initialize() 在 Microsoft.Data.Sqlite.SqliteConnection..cctor()
堆栈跟踪 = 在 Microsoft.Data.Sqlite.SqliteConnection..ctor(String 连接字符串)在 CartographerYou.MainPage.InsertMapRecord(字符串 mapName, String mapNotes, Int32 preferredZoomLevel) at CartographerYou.MainPage.btnCre8NewMap_Click(对象发送者, RoutedEventArgs 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()?
【问题讨论】:
-
StackOverflow 上有一个类似的thread。看来问题是由于缺少必需的参考资料引起的。它错过了 SQLitePCL.raw* 引用。您可以尝试重新安装 NuGet 包或尝试那里发布的其他解决方案。
-
使用 SQLitePCL 给了我一开始的狂热。我用谷歌搜索“什么是 SQLitePCL”并得到了这个:SQLitePCL.raw 是一个可移植类库 (PCL),用于对 SQLite 进行低级(原始)访问。此包不提供对应用程序开发人员友好的 API。相反,它提供了一个处理平台和配置问题的 API,可以在此基础上构建更友好的 API。
-
嗯,该线程上的解决方案是重新安装
Microsoft.Data.SQLiteNuGet 包,以确保正确安装所有必需的引用。请检查@Tristan Trainer 发布的解决方案。或者你可以试试@René Schindhelm 的解决方案 -
很好,重新安装(或实际上)安装让我解决了这个问题。事实证明,我安装了 Microsoft.Data.SQLite.Core,但没有安装 Microsoft.Data.SQLite;现在我也有后者(版本 5.0.1)。我收到了不同的错误消息,但这是向前迈出的一步。
-
另外:你必须在打开连接之前调用
SQLitePCL.raw.SetProvider()