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
- Working with local databases in Windows Phone 8 Part I
- Working with local databases in Windows Phone 8 Part II
- Working with local database in Windows Phone 8- Part III