从Windows应用商店导出Sqlite数据库 [英] Export Sqlite database from Windows Store App

查看:95
本文介绍了从Windows应用商店导出Sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Sqlite数据库,该数据库填充在Windows Store应用程序中.现在,我想在重新安装应用程序时说数据库可用,因此,如果正确的话,我必须将其作为文件添加到我的项目中.

I've got a Sqlite database which I populate inside a Windows Store app. Now I'd like to have said database availiable when reinstalling the app, therefore I have to add it to my project as a file, if I'm right.

现在,我正在尝试使用PickSaveFileAsync()-Method导出数据库文件.不幸的是,一旦获得文件,我真的不知道该怎么做.这是到目前为止我得到的:

Now I'm trying to export the database file using the PickSaveFileAsync()-Method. Unfortunately I don't really know how to go about this once I've got the file. Here's what I've got so far:

    private async void exportDB_Click(object sender, RoutedEventArgs e)
    {
        StorageFile database = await ApplicationData.Current.LocalFolder.GetFileAsync("Hashes.db");
        var picker = new FileSavePicker();
        picker.FileTypeChoices.Add("Database File", new string[] { ".db" });
        picker.CommitButtonText = "Save DB";
        picker.SuggestedFileName = "Database";
        StorageFile file = await picker.PickSaveFileAsync();
        if (file != null)
        {
            // What to do here?
        }
    }

推荐答案

我很难弄清楚您要实现的目标.

I'm having a hard time figuring out what exactly you are trying to achieve.

看一下代码,您似乎想将数据库文件复制到用户定义的位置.为此,只需在if块内添加以下代码:

Looking at the code it seems that you want to copy your database file to a user defined location. To do this just add the following code inside your if block:

using (var srcStream = await database.OpenStreamForReadAsync())
{
    using (var destStream = await file.OpenStreamForWriteAsync())
    {
        await srcStream.CopyToAsync(destStream);
    }
}

在阅读介绍性文字时,您似乎正在尝试将文件包含在应用程序中.在这种情况下,您确实应该将其作为Content添加到项目中.然后,您可以从以下代码中访问它:

Reading the introductory text it seems you're trying to include the file with your application. In this case you should indeed add it as Content to your project. You can then access it from code as follows:

var dbFile = Package.Current.InstalledLocation.GetFileAsync(@"db\Hashes.db");

如果您不知道,可以在以下目录的LocalFolder中找到文件:

In case you didn't know, you can find the files in LocalFolder in the following directory:

C:\Users\Username\AppData\Local\Packages\PackageName\LocalState

这篇关于从Windows应用商店导出Sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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