C# SQLite可以';我找不到桌子
我在App.xaml.cs中有以下代码:C# SQLite可以';我找不到桌子,c#,wpf,sqlite,mvvm,C#,Wpf,Sqlite,Mvvm,我在App.xaml.cs中有以下代码: private SQLiteConnection dbCon; public void App_Startup(object sender, StartupEventArgs e) { SQLiteConnection.CreateFile("testdb.sqlite"); dbCon = new SQLiteConnection("Data Source=testdb.sqlite;Vers
private SQLiteConnection dbCon;
public void App_Startup(object sender, StartupEventArgs e)
{
SQLiteConnection.CreateFile("testdb.sqlite");
dbCon = new SQLiteConnection("Data Source=testdb.sqlite;Version=3;");
dbCon.Open();
string query = "create table if not exists Settings(Key nvarchar(max), Value nvarchar(max));";
SQLiteCommand command = new SQLiteCommand(query, dbCon);
command.ExecuteNonQuery();
}
这运行得很好。。。但在ShellViewModel.cs上,我有以下内容:
public ShellViewModel()
{
dbCon = new SQLiteConnection("Data Source=testdb.sqlite;Version=3;");
dbCon.Open();
string query = "Select Value from Settings where (Key = 'isFirstTime')";
SQLiteCommand command = new SQLiteCommand(query, dbCon);
command.ExecuteNonQuery();
//if(no information or first time)
// show registerationview.xaml
//else
this.ActivateItem(new MainViewModel()); // show main view.
}
问题是我在上面的代码中得到了“Table not found”
我能想到的唯一原因是,App.xaml.cs
位于项目的根目录中,而ShellViewModel.cs
位于ViewModels
文件夹中,但我不知道如何让它指向根目录中的特定数据库文件,如果这是问题的话
问题/解决方案是什么?
我试图找到答案,但所有问题的答案都是“问题的可能原因”,因此它们没有真正的帮助。在使用SQLite数据库的所有位置使用完整路径,如下所示
string fullPath = Path.Combine(ApplicationDataBaseFolderPath, "testdb.sqlite");
SQLiteConnection.CreateFile(fullPath);
及
您可以使用SO问题的其中一个答案,找到应用程序目录,然后将其与数据库应该位于的文件夹名称组合
string ApplicationDataBaseFolderPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "AppData");
谢谢什么是
ApplicationDataBaseFolderPath
?它没有被识别。
string ApplicationDataBaseFolderPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "AppData");