Click here to Skip to main content
15,890,717 members
Articles / Mobile Apps / Windows Phone 7
Technical Blog

How to Use Existing Database in Windows Phone

Rate me:
Please Sign up or sign in to vote.
4.79/5 (4 votes)
5 May 2013MIT 10.5K   7   3
Using existing database in Windows Phone

Introduction

Normally in Windows Phone apps, we used to create a database in the Application Launch event, like the following:

C#
if (!dataContext.DatabaseExists())
{
	dataContext.CreateDatabase();
}

And if there are any master tables, you can write code to insert after database creation, like this:

C#
if (!dataContext.DatabaseExists())
{
    dataContext.CreateDatabase();
    dataContext.Categories.InsertAllOnSubmit(
        new[] { DefaultCategory });
    dataContext.SubmitChanges();
}

This approach is not feasible if you have a lot of data, for example a Dictionary database. In such scenarios, you can add the existing database to the project, and set the Build Action to Content.

Properties Window - Build Action - Content

This will deploy the database file with your application onto the phone, but it will be placed in the same folder as all other static content for your application. Your application can only read from this folder.

You can communicate to the existing database using the following connection string:

C#
private const string ConnectionString = 
"Data Source ='appdata:/Database/Dictionary.sdf';File Mode=read only;";

If you want to modify the database, you need to copy (duplicate) the database to the application isolated storage. Here is the code snippet which will help you to copy your database file to isolated storage.

C#
const string DatabasePath = "Database";
const string Filename = @"Database/Dictionary.sdf";
using (var isolatedStorageFile = 
    IsolatedStorageFile.GetUserStoreForApplication())
{
    if (!isolatedStorageFile.DirectoryExists(DatabasePath))
    {
        isolatedStorageFile.CreateDirectory(DatabasePath);
    }

    if (isolatedStorageFile.FileExists(Filename))
    {
        return;
    }

    var resource = 
        Application.GetResourceStream(new Uri(Filename, UriKind.Relative));
    using (var file = isolatedStorageFile.CreateFile(Filename))
    {
        var length = resource.Stream.Length;
        var buffer = new byte[1024];
        var readCount = 0;
        using (var binaryReader = new BinaryReader(resource.Stream))
        {
            while (readCount < length)
            {
                int actual = binaryReader.Read(buffer, 0, buffer.Length);
                readCount += actual;
                file.Write(buffer, 0, actual);
            }
        }
    }
}

Happy programming!

Related Content

  1. Isolated Storage in C#
  2. Uploading Files using Webservice
  3. Upload multiple files using Silverlight
  4. How to Store and Retrieve files from SQL Server Database
  5. Quick introduction to SQLite with C#

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Technical Lead
India India
Working as Tech. Lead

My blog : dotnetthoughts.net.
You can follow me in twitter : @anuraj

Comments and Discussions

 
QuestionUse can either take advantage of StorageFile.CopyAsync to copy files Pin
Suraj Sharma (suraj)26-Dec-13 5:01
Suraj Sharma (suraj)26-Dec-13 5:01 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA13-Jun-13 20:48
professionalȘtefan-Mihai MOGA13-Jun-13 20:48 
GeneralMy vote of 3 Pin
mafaz32115-May-13 17:41
professionalmafaz32115-May-13 17:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.