Windows-8.1

Working with local database in Windows Phone 8- Part III

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

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

Accessing the Isolated Storage Data of developer apps installed to emulator or unlocked Windows Phone device

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.

 

Note:

  • 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.

Download full project Database-Part-3.zip

Vivek Maskara

I am pursuing BTech in Software Engineering from Delhi Technological University. I develop apps, create websites and blog about my app experiences.

  • Eric

    The Update feature does not work.

    • I will check it again. Are you sure that you added a few rows before trying to update records?

      • Eric

        Yes I did.

      • Eric

        The Update page shows up blank after it loads.

  • Marcin

    I have problem with The Upadate, too.

    Error:

    Warning 1 Field ‘Databases.Update.nam’ is never assigned to, and will always have its default value null C:Users…Update.xaml.cs 15 20 Databases

    • Thanks for pointing that out. We have updated the source code file to fix the bug.

  • Marcin

    I have problem with The Upadate, too.

    Error:

    Warning 1 Field ‘Databases.Update.nam’ is never assigned to, and will always have its default value null C:Users…Update.xaml.cs 15 20 Databases

    • Thanks for pointing that out. We have updated the source code file to fix the bug. We forgot to assign the textbox’s values to the parameters being passed.

  • Robert

    Trying your windows phone app. Question: is there any chance you can do the code in VB.Net? Not much time to learn C#. Also, I do not see in your code where there is a “search” function. Thanks in advance.

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding
Close