Microsoft.Data.Sqlite.SqliteException: 'SQLite 错误 14:'无法打开数据库文件'. [英] Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'

查看:65
本文介绍了Microsoft.Data.Sqlite.SqliteException: 'SQLite 错误 14:'无法打开数据库文件'.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误 Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.当我尝试运行此代码时,它是一个 UWP 应用程序,我正在使用 sqlite

I get this error Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.' when I try to run this code, it is a UWP application and I am using 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\BobDB.db.将此文件的 Build Action 设置为 Content.好消息是我们的文件现在包含在我们的已安装应用程序文件夹中.不好的是它是 只读.为了克服它,我们需要将其复制到本地应用程序文件夹:

UWP apps are running in the sandbox and when you run them they are installed into it. They are not running in your source code bin folder of your project. In order to make your code up and running add your db file to your project Assets folder Assets\BobDB.db. Set Build Action of this file to Content. The good thing is that our file is now included to our Installed app folder. The bad thing is that it is read only. To overcome it we need to copy it to local app folder:

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();
...

这篇关于Microsoft.Data.Sqlite.SqliteException: 'SQLite 错误 14:'无法打开数据库文件'.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆