【问题标题】:Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'Microsoft.Data.Sqlite.SqliteException:'SQLite 错误 14:'无法打开数据库文件'。
【发布时间】:2020-07-03 13:28:48
【问题描述】:

我收到此错误 Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'。当我尝试运行此代码时,它是一个 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();
            }
        }

【问题讨论】:

  • 你读过this吗?
  • 我已经读过了,但由于数据库在应用程序文件夹中,它应该有任何问题。
  • 也许您从未指定版本? using (SqliteConnection conn = new SqliteConnection("Data Source = F:\Curtis\Documents\Capstone\Capstone\Database\BobDB.db;Version=3;"))
  • 这给出了错误关键字不支持'版本'
  • 您是否检查过您的 Windows 用户是否有权访问此文件?另外,据我所知,UWP 应用程序安装在 C:\Program Files\WindowsApps 文件夹中的某个位置。

标签: c# sqlite uwp


【解决方案1】:

UWP 应用在沙盒中运行,当您运行它们时,它们会安装到其中。它们没有在项目的源代码 bin 文件夹中运行。为了使您的代码启动并运行,请将您的 db 文件添加到您的项目资产文件夹Assets\BobDB.db。 将此文件的构建操作设置为Content。 好消息是我们的文件现在包含在我们的已安装应用程序文件夹中。不好的是它是read only。为了克服它,我们需要将它复制到本地应用程序文件夹:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2013-01-03
    • 2021-10-15
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2011-05-01
    相关资源
    最近更新 更多