This is the fourth post of the Windows Phone’s local database series and I will discuss how to move referential database to isolated storage. Here are the links to other posts in this series
- Working with local database in Windows Phone 8- Introduction
- Working with local database in Windows Phone 8- Part I
- Working with local database in Windows Phone- Part II
In this post we will discuss how you could move a referential database from app’s local storage (resources) to isolated storage. It is not very commonly used but are very useful in some situations.
Moving a referential database from app’s local storage to isolated storage
This is particularly useful when one feels the need to preload the database with some data when the app is first launched rather than creating a new database and inserting values in it. When the amount of data you wish to preload is quite large then it is beneficial to put the database containing the preloaded data in app’s local storage (app resources) and then move it to the isolated storage when the app is launched. You would remember from previous tutorials that in the constructor of the default navigation page we have added a condition that checks whether a database exists. If it doesn’t then a new database is created. So instead of creating a new database we need to move the referential database. Here’s how it can be done.
Step 1. Run the app and load the database with desired data
Firstly, while debugging you need to run the app and load it with all the data you wish to preload. Here we will use the same sample application that we have been using in this series. Let us add a few users in the database. You could see that we have added three users in the database.
Step 2. Use ISETool to export the database to your computer
You need to copy the database from app’s isolated storage to your computer’s folder. Follow this article ti easily copy files using ISETool
Here’s what you need to do in command prompt. Two simple commands are all you require! You will get the files in the path you specified, here c:\data\myfiles
Note: If you are using a windows phone 8 emulator then the path should have v8.0 in it otherwise it should have v8.1 for windows phone 8.1 emulator. I was using an 8.1 emulator so I had to change the path from v8.0 to v8.1.
Step 3. Locate the database on your computer and import it in your project
As specified the files will be located inside c:\data\myfiles\IsolatedStorage folder.
Right click on project name in solution explorer to add an existing item.
In the dialog box that appears, navigate to the folder containing the copied database and add it to your project.
Step 4. Add the method MoveReferenceDatabase() to the MainPage.xaml.cs of your project.
This function copies the specified file from app resources to isolated storage. We will call this function instead of creating a new database when no existing database is found in our project. This function is executed only when the app is run for the first time or whenever no database is found.
Step 5. Replace the create database statement with a call to MoveReferenceDatabase()
In the constructor of MainPage.xaml.cs where we check for the existence of a database, replace the statement that creates a new database when none exists with a call to MoveReferenceDatabase(). This will copy the database from app’s resources to isolated storage.
- The name of the database copied should match with name specified in the connection string variable of data context class.
- The schema of the copied database should be exactly the same as specified in the app.
- The copied database will contain all the preloaded data in it.
- It should be used cautiously and care must be taken to ensure that any triggers or actions on database update is consistent with the copied database. For example, if the local data is synced with some server, then care must be taken to maintain consistency
You can get the source code from the link below.