Working with local databases in Windows Phone 8

In this article I am going to explain how you could use the Isolated Storage by creating a local referential database which can be accessed locally by that Windows Phone application. Database operations could be performed using LINQ to SQL or using SQLite. Usually developers prefer LINQ to SQL for DB operations but SQLite becomes a better option when you plan to make apps both for Windows Phone and Windows 8. Currently Windows 8 supports databases using SQLite only, so if you don’t wish to rewrite the code for both platforms you should prefer SQLite.

LINQ to SQL object model uses the System.Data.Linq.DataContext namespace to basically make a proxy call to the local database in the Isolated Storage container. LINQ to SQL Runtime plays as a bridge between the data context object and the real data to do the manipulations based on the user selection. Here are a few points which you should keep in mind while making databases for your app

  • Database file is stored in Isolated Storage Container
  • The database is available only for the application targeted as it is isolated from other apps
  • LINQ is used for queries
  • Local database feature can be accessed directly by adding System.Data.Linq assembly only since primary support is available with the framework.
  • Connection string much be used in the format of “Data Source =’isostore:/DirectoryName/Databasename.sdf”;

We are going to perform the following tasks to get a good grasp on using databases

  • Creating a local database
  • Adding data to the local database
  • Fetching data from local database
  • Deleting data from the local database
  • Deleting a local database

Before you create a local database, you need to create a new project and add the required references to it.

Step 1. Create a new project `Database’ targeted for Windows Phone 8

Open Visual Studio and create a black Silverlight Windows Phone app.

 


 

Step 2. Check and add a reference to System.Data.Linq to your project

Right Click on References in your project’s Solution Explorer and click on Add Reference and

 

Browse and choose System.Data.Linq and add it to your project. I am not sure if this reference would be already added. If you are using the latest VS(currently VS 13 update 1) then it may be already referenced in your project as it was in mine. Otherwise you need to add this reference.

 

 

You could right click on Windows Phone under References to open the object browser and check if a reference to System.Data.Linq already exists.

 

 

Now you are all set to create a local database in your app. In my next post on databases I will explain how you can create, add and fetch from a database. I will update this post with the links to those posts.

UPDATE: New articles in this series

 

Vivek Maskara

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

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