Ibraheem Osama Mohamed (ابراهيم اسامه محمد)

Welcome to my blog :)

Window Phone 7 Database.

leave a comment »

WP7 is a very easy mobile operating system to deal with.Once I was developing an application on WP7 that uses the database I stayed very long time to understand the concept of the WP7 database and really making database on WP7 is very complex.But at last I did it and developed the application so I decided to write this blog to make it easy one every one to make the database.

First : WP7 database based on Microsoft LINQ so you need to know what is the LINQ and how you can use it this is two linq that may helps you


Second : SQL Server Compact Edition you had to make your Database a Compact Edition (that can run on the WP7 without installing SQL Server)

Third : Convert From Database To C# “LINQ to SQL” and this is 2 link that will help you

Hint : You can’t make LINQ to SQL Class in WP7 application Directly so you need to make it in new C# application then add it to your app

Hint : All LINQ to SQL queries occur via a DataContext class, which controls the flow of data between the program and the database. A s

1)Creating the SQL Server Compact Edition Database : Design the database you want.

2)Open New WPF application.

3)Right Click Add New Item Go to Data then LINQ to SQL Class name it Mydatabase.

3)Now the Visual Studio will open you Mydatabase.dbml.

4)Open the solution Explorer then navigate to your database

5)Drag your Database into the opened Window

6)Visual Studio will  generate the tables and it’s relation

7)Then expand Mydatabase.dbml you’ll find a class.cs”Context Class”

8)Copy the class from the project NOT From the solution explorer

9)Past the context class on your new WP7 application

10)Open the context class you’ll find errors on some “linq” words so you need to add a reference system.data.linq “right click on the project add reference the you’ll find a windows which contains many name spaces add the needed namespace”

11)Then you will find some errors on some function remove it “the functions on the yellow box”

Note: Take a look at the context class and you’ll understand you database from it ,Imagine you had to write this class by code and that is very difficult “tables, primary keys, allow null property …..”  And what if the database is big?? You’ll write all it in code.

Lets jump into the Coding part

1)Define a connection string

2)Private const string ConnectionString = @”isostore:/database.sdf”; // isostore means the isolated storage

3)Then initialaizae the context class in your code (not the database.cs the class in it)

public MainPage()



using (DataClasses1DataContext context=new DataClasses1DataContext(ConnectionString))


if (!context.DatabaseExists())


// create database if it does not exist





Note :Very Important note the linq uses the context class not the database you made so you must use the tables as in the context class not in the database you made.

4)This is the code makes you add to the database

using (DataClasses1DataContext context = new DataClasses1DataContext(ConnectionString))


// create a new country and add it to the context

Info employeeInfo = new Info();

employeeInfo.Name = “whatever you want”;

employeeInfo.id= 123456;

// add the new country to the context


// save changes to the database



5)Return the data

 IList<Employee> cityList = null;

using (CountriesDataContext context = new CountriesDataContext(ConnectionString))


IQueryable<Employee> query = from c in context.Employees select c;

cityList = query.ToList();


6)Show Info

System.Text.StringBuilder messageBuilder = new System.Text.StringBuilder();

foreach (Info n in data)


messageBuilder.AppendLine(n.Name + ”  ” + n.id);



7)Write your functions and then add the database code as you want.

I hope that you get the idea from the SQL Compact and LINQ.

Ibraheem Osama Mohamed
Mobile Developer-Rashdan It || Microsoft Student Partner -Microsoft Egypt

Written by Ibraheem Osama Mohamed

March 16, 2012 at 10:03 am

Posted in Silverlight, WP7

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: