sqlite - 数据库 synchro/bakup - windows 10 手机

sqlite - database synchro/bakup - windows 10 mobile

我正在制作 windows 10 个应用程序 - 销售经理。只有3张桌子。我有本地 sqlite 数据库。我正在寻找备份此数据库或同步的简单方法。因为数据库很小,所以我总是考虑完全同步,所以例如:我点击 'Send database' 并且来自设备的完整数据被发送到服务器。当我单击 'download' 时,将下载所有数据库。我将不胜感激建议简单的方法来保存数据库

恕我直言,您可以利用 OneDrive's App Folder 来备份您的 SQLite 数据库。

The App Folder is a dedicated, special folder for your app. The App Folder is typically named after your app, and is found in the Apps folder in the user's OneDrive. If you request the onedrive.appfolder permission scope and the user authorizes it, your app gets read and write access to this folder.

对于 C#,您可以在应用程序中使用 OneDrive SDK for CSharp。例如,您可以像下面这样发送您的数据库文件:

private async Task<Item> UploadDBFile()
{
    StorageFile DBFile = await ApplicationData.Current.LocalFolder.GetFileAsync("db.sqlite");

    var oneDriveClient = await OneDriveClientExtensions.GetAuthenticatedUniversalClient(new[] { "onedrive.appfolder" });

    using (var contentStream = await DBFile.OpenStreamForReadAsync())
    {
        return await oneDriveClient.Drive.Special.AppRoot.ItemWithPath("backup.sqlite").Content.Request().PutAsync<Item>(contentStream);
    }
}

然后像这样下载数据库:

private async Task DownloadDBFile()
{
    var oneDriveClient = await OneDriveClientExtensions.GetAuthenticatedUniversalClient(new[] { "onedrive.appfolder" });

    using (var contentStream = await oneDriveClient.Drive.Special.AppRoot.ItemWithPath("backup.sqlite").Content.Request().GetAsync())
    {
        StorageFile downloadedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("DownloadedDB.sqlite",
CreationCollisionOption.ReplaceExisting);
        using (var writerStream = await downloadedFile.OpenStreamForWriteAsync())
        {
            contentStream.CopyTo(writerStream);
        }
    };
}

请注意,要在您的应用程序中使用 OneDrive API,您需要先通过以下 these steps.

注册您的 OneDrive 应用程序

要同步本地数据库,您也可以尝试Offline Data Sync in Azure Mobile Apps。这比备份数据库更强大。

When your app is in offline mode, users can still create and modify data, which will be saved to a local store. When the app is back online, it can synchronize local changes with your Azure Mobile App backend. The feature also includes support for detecting conflicts when the same record is changed on both the client and the backend. Conflicts can then be handled either on the server or the client.

Offline sync has a number of benefits:

  • Improve app responsiveness by caching server data locally on the device
  • Create robust apps that remain useful when there are network issues
  • Allow end-users to create and modify data even when there is no network access, supporting scenarios with little or no connectivity
  • Sync data across multiple devices and detect conflicts when the same record is modified by two devices
  • Limit network use on high-latency or metered networks

有关详细信息,请参阅 Offline Data Sync in Azure Mobile Apps为您的 Windows 应用启用离线同步,以及 Todo 离线示例 在 GitHub.