簡體   English   中英

Microsoft.Data.Sqlite.SqliteException: 'SQLite 錯誤 14:'無法打開數據庫文件'。

[英]Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'

我收到此錯誤 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();
            }
        }

UWP 應用程序在沙箱中運行,當您運行它們時,它們會安裝到沙箱中。 它們沒有在項目的源代碼 bin 文件夾中運行。 為了使您的代碼啟動並運行,請將您的 db 文件添加到您的項目 Assets 文件夾Assets\\BobDB.db 將此文件的 Build Action 設置為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();
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM