sqlite - database synchro/bakup - windows 10 mobile - c#

I'm making simply windows 10 apllication - sales manager. Only 3 tables. I have local sqlite database. I'm looking for easy way to backup this database or synchronize. Because databae is small I think about full synchro always, so for example: I'm clicking 'Send database' and full data from device is send to server. When I click 'download' all database will be downloaded. I will be grateful for advice simply way to save database

IMHO, you can take advantage of OneDrive's App Folder to backup your SQLite database.
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.
And for C#, you can use OneDrive SDK for CSharp in your application. For example, you can send your database file like following:
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);
}
}
And download the db like:
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);
}
};
}
Please note that to use OneDrive API in your application, you need to first register your application for OneDrive by following these steps.
To sync local database, you can also try with Offline Data Sync in Azure Mobile Apps. This is more powerful than backup your database.
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
For more info, please refer to Offline Data Sync in Azure Mobile Apps, Enable offline sync for your Windows app, and the Todo offline sample in GitHub.

Related

How do you place an exisiting SQLite database into an application's local data store for use in a Universal Windows Platform application?

I have successfully followed the instructions to work with SQLite databases in a Universal Windows Platform application, as explained here: "Use a SQLite database in a UWP app"
That tutorial shows how to create a new SQLite database, with C# code, in the application's local data store, but it does not explain how to put an existing SQLite database into the local data store.
how to put an existing SQLite database into the local data store.
The database is also a db file, so this question can be translated into how to copy file to local storage.
Here is the code:
public async Task<StorageFile> CopyFileToLocalStorage(StorageFile dbFile)
{
var localFolder = ApplicationData.Current.LocalFolder;
var localFile = await dbFile.CopyAsync(localFolder, "sqliteSample.db",NameCollisionOption.ReplaceExisting);
return localFile;
}
Best regards.

Uploading an image to Microsoft Cognitive Services?

I am trying to post an image to the Computer Vision API of Microsoft Cognitive Services. It requires me to upload the image as an url. I have the uploaded image by the user with an URI like http://localhost:9000/content/8a684db8?file=IMG-20160503-WA0002.jpg on my local pc. I tried the obvious but that doesn't work. How to pass the image to their API?
They also mention I can post the image as a raw binary but I am unable to get how to get going.
PS: You can get the subscription keys using the free subscriptions if you want to test it for some other cases.
localhost is 127.0.0.1, e.g. your PC when accessing from your PC. You should pass external IP of your PC in the internet
Well I was able to get a solution. Didn't post my answer sorry.
Microsoft Computer Vision Documentation This shows how to call their API's using the nuget Microsoft.ProjectOxford.Vision.The below code uploads and analyzes a locally stored image to the analyze endpoint of the Computer Vision API service.
using Microsoft.ProjectOxford.Vision;
using Microsoft.ProjectOxford.Vision.Contract;
private async Task<AnalysisResult> UploadAndAnalyzeImage(string imageFilePath)
{
//
// Create Project Oxford Computer Vision API Service client
//
VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey);
Log("VisionServiceClient is created");
using (Stream imageFileStream = File.OpenRead(imageFilePath))
{
//
// Analyze the image for all visual features
//
Log("Calling VisionServiceClient.AnalyzeImageAsync()...");
VisualFeature[] visualFeatures = new VisualFeature[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags };
AnalysisResult analysisResult = await VisionServiceClient.AnalyzeImageAsync(imageFileStream, visualFeatures);
return analysisResult;
}
}
On this Git Repository you can see some samples.Here you also get how you can handle client errors and exceptions.

my roaming data dosen't sync between devices

I made a UWP application. I use the roamingdata.I save the setting by this:
public static void WriteCode(string pwd)
{
ApplicationDataContainer RoamingSettings = ApplicationData.Current.RoamingSettings;
RoamingSettings.Values["Code"] = EncryptHelper.PwdEncrypt(pwd);
}
I read the setting by this:
public static string GetCode()
{
ApplicationDataContainer RoamingSettings = ApplicationData.Current.RoamingSettings;
string str = (String)RoamingSettings.Values["Code"];
if (!String.IsNullOrEmpty(str))
return str;
else
return EncryptHelper.PwdEncrypt("123");
}
I complete the application and upload to windows store and passed check. Then I download this app on my phone.
I change the ApplicationData.Current.RoamingSettings on phone. I write something to the ApplicationData.Current.RoamingFolder on my phone.
Next I closed the app on my phone and download the app on my PC. But when I opened the app on my PC, I found that the ApplicationData.Current.RoamingSettings and the ApplicationData.Current.RoamingFolder didn't change anything.
I checked the C:\Users\XXX\AppData\Local\Packages\XXX\RoamingState on my PC,there was nothing. I checked C:\Users\XXX\AppData\Local\Packages\XX\Settings on my PC,there was roaming.lock and settings.dat. But I can't read the lastest settings and roaming data that I haved added on my phone anymore.
I have waited for 2 hours, there was no change on my PC.
there is something that I should state first:
1 All the deploy work was done by windows store.
2 I check my PC application setting after closed the app on phone. I even shut down my mobile phone to observe the change to my PC.
what's wrong with my code? or what's wrong with the roamingdata mechanism? I need an answer, thank you!
The code you've posted is right. However, there are some possible reasons for the failure of roaming data sync:
Any user can benefit from roaming app data if they use a Microsoft account to log on to their device. However, users and group policy administrators can switch off roaming app data on a device at any time. If a user chooses not to use a Microsoft account or disables roaming data capabilities, she will still be able to use your app, but app data be local to each device.
Keep in mind that roaming data is associated with a user's Microsoft account. Roaming data will only sync if a user logs into her devices using the same Microsoft account and installs the app on several devices.
Don't use roaming for data that relies on instant syncing. Windows doesn't guarantee an instant sync; roaming could be significantly delayed if a user is offline or on a high latency network.
Roaming of settings is not instant. The system weighs several factors when determining when to send the data. We can detect whether new roaming data has arrived on the local device by listening for the ApplicationData.DataChanged event. This event occurs when app data has just finished syncing from the cloud. Any time a device receives new roaming data, the DataChanged event will fire, passing in the updated ApplicationData object. This lets us make any adjustments to our app when data has changed.
For important, time critical settings, use the HighPriority setting associated with RoamingSettings like following:
// High Priority setting, for example, last page position in book reader app
roamingSettings.values["HighPriority"] = "65";
This is a special key in the roaming settings we can use for data we need to sync immediately. Adding HighPriority to any setting will have it synced as quickly as possible.
Don't roam large sets of app data. There's a limit to the amount of app data an app may roam; use RoamingStorageQuota property to get this maximum. If an app hits this limit, no data can roam until the size of the app data store no longer exceeds the limit.
The name of each setting can be 255 characters in length at most. Each setting can be up to 8K bytes in size and each composite setting can be up to 64K bytes in size. The sync engine may limit the total size of settings and files that can roam. It’s important to keep track of the amount of data you’re attempting to roam. If the total amount of data you’re attempting to sync exceeds the limit, then nothing will sync between the devices.
App data only roams between installed apps with the same version number. For example, devices on version 2 will transition data between each other and devices on version 3 will do the same, but no roaming will occur between a device running version 2 and a device running version 3. If you install a new app that utilized various version numbers on other devices, the newly installed app will sync the app data associated with the highest version number.
If you are using versioning in your roaming date, please make sure you are dealing with the right version.
These are some possible reasons that can cause roaming data doesn't sync between devices. For more info, please check Roaming data in Store and retrieve settings and other app data.

Sqlite Remote Manangment and multiple record

first hi all,
i got a db sqlite in my project also got xml files and i translate data from xml and save to local db,
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string localFilename = "personel.xml";
string localPath = Path.Combine(documentsPath, localFilename);
this is my local db, im planing to move db3 to iss, when i move should i give
localpath like = 192.23.21.2/www/personel.xml?
or should i add more things like http://docs.xamarin.com/guides/cross-platform/application_fundamentals/web_services/ ?
and second question
what if multiple users update or delete from table?
there is a one db3 file and same time 3 connections how can i manage it?
SQLite is not designed to work as a multi-user database.
If you have many client programs accessing a common database over a
network, you should consider using a client/server database engine
instead of SQLite.
It is also a really bad idea to allow your mobile client app to talk directly to a remote database. This is why web services are usually recommended as an additional layer between your client and a central, remote database. They allow you to introduce an additional layer of control and security over your data access.

C# Windows Store BackgroundDownloader to removable storage

I am working on an app that will run on all Windows 8 devices (RT support a must) and I am working on adding some offline capabilities, but I can't figure out how to download to a removable storage device such as a USB drive or, in the case of a Surface RT, the micro SD card. Ideally I would like to be able to have the user specify the directory, but it may end up downloading hundreds of files so it has to be specified just once, not once per file. I also want to avoid requiring the user to manually configure libraries.
I have found plenty of articles about how to download to the various libraries, but those go to the internal storage and thus has very limited space on a Surface RT. How can I have the user specify a location for a large number of files to download and/or download to a removable storage device?
A really slick solution would be a way to programmatically create a library in a location of the user's choosing so the user can choose if they want it on the local system or on a removable device.
I appreciate any suggestions.
You should take advantage of FutureAccessList. It allows you to reuse files and folders that the user has previously granted you access to.
First the user will select the target folder using a FolderPicker:
var picker = new FolderPicker();
picker.FileTypeFilter.Add("*");
var folder = await picker.PickSingleFolderAsync();
You then add the folder to FutureAccessList and get back a string token which you can store for later use (e.g. to ApplicationData.LocalSettings):
var token = StorageApplicationPermissions.FutureAccessList.Add(folder);
When you want to download a file, first get the folder from FutureAccessList and create the target file:
var folder = await StorageApplicationPermissions.FutureAccessList
.GetFolderAsync(token);
var file = await folder.CreateFileAsync(filename);
With that data you can create a DownloadOperation:
var downloader = new BackgroundDownloader();
var download = downloader.CreateDownload(uri, file);
From here on proceed as if you were downloading to any other location (start the download, monitor progress...).

Categories

Resources