我正在关注 this教程,以使用SQLite数据库创建应用程序。
当我执行它时,我得到 System.IO.FileNotFoundException 指向文件 SQLite.cs ,发生错误的行显示在下面的屏幕截图中(在SQLite.cs文件中)
以下是用于创建数据库的代码段
string dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
if (!FileExists(dbPath).Result)
{
using (var db = new SQLiteConnection(dbPath))
{
db.CreateTable<Person>();
}
}
和方法FileExists
private async Task<bool> FileExists(string fileName)
{
var result = false;
try
{
var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
result =true;
}
catch
{
}
return result;
}
我想知道出了什么问题。有什么帮助吗?
答案 0 :(得分:1)
使用行if (!FileExists(dbPath).Result)
,您说如果文件不存在,请连接到数据库并创建表。所以你的情况是错误的,因为行
Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName)
希望现有文件正确执行。
使用以下内容纠正您的if
条件:
if (FileExists(dbPath).Result)