我收到此错误 Microsoft.Data.Sqlite.SqliteException:“SQLite 错误 14:‘无法打开数据库文件’。”当我尝试运行此代码时,它是一个 UWP 应用程序,并且我正在使用 sqlite
private void btnContinue_Click(object sender, RoutedEventArgs e)
{
string datasource = @"F:\Curtis\Documents\Capstone\Capstone\Database\BobDB.db"; ;
using (SqliteConnection conn = new SqliteConnection(@"Data Source = " + datasource))
{
conn.Open();
SqliteCommand command = conn.CreateCommand();
command.CommandText = "Select TestTableTXT from TestTable;";
using (SqliteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
DatabaseTextBlock.Text = reader.GetString(0);
}
}
conn.Close();
}
}
UWP 应用程序在沙箱中运行,当您运行它们时,它们会安装到沙箱中。它们没有在项目的源代码 bin 文件夹中运行。为了使您的代码正常运行,请将您的 db 文件添加到项目的 Assets 文件夹中Assets\BobDB.db
。
将此文件的构建操作设置为Content
。
好处是我们的文件现在包含在我们的已安装应用程序文件夹中。糟糕的是它是。为了克服这个问题,我们需要将其复制到本地应用程序文件夹:
public MainPage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
}
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
string targetDbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Database\\BobDB.db");
if (!File.Exists(targetDbPath))
{
var installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
using (var input = await installedLocation.OpenStreamForReadAsync("Assets\\BobDB.db"))
{
using (var output = await Windows.Storage.ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("Database\\BobDB.db", Windows.Storage.CreationCollisionOption.FailIfExists))
{
await input.CopyToAsync(output);
}
}
}
using (SqliteConnection conn = new SqliteConnection(@"Data Source = " + targetDbPath))
{
conn.Open();
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)