Windows Phone Mango Local Database(SQL CE): Creating the Database
published on: 9/15/2011 | Views: N/A | Tags: LocalDB Mango
by WindowsPhoneGeek
This is the 10th post from the "Windows Phone Mango Local Database" series of short posts that will cover all you need to know in order to get started using a Local Database in Windows Phone 7.1 Mango. This time I am going to talk about creating a Windows Phone 7.1 Mango local database.
Here is what else is included in this series:
- Windows Phone Mango Local Database(SQL CE): Introduction
- Windows Phone Mango Local Database(SQL CE): Linq to SQL
- Windows Phone Mango Local Database(SQL CE): [Table] attribute
- Windows Phone Mango Local Database(SQL CE): [Column] attribute
- Windows Phone Mango Local Database(SQL CE): [Association] attribute
- Windows Phone Mango Local Database(SQL CE): [Index] attribute
- Windows Phone Mango Local Database(SQL CE): Database mapping
- Windows Phone Mango Local Database(SQL CE): DataContext
- Windows Phone Mango Local Database(SQL CE): Connection Strings
- Windows Phone Mango Local Database(SQL CE): Creating the Database
- Windows Phone Mango Local Database(SQL CE): Database Queries with LINQ
- Windows Phone Mango Local Database(SQL CE): How to Insert data
- Windows Phone Mango Local Database(SQL CE): How to Update data
- Windows Phone Mango Local Database(SQL CE): How to Delete data
Creating the Database
After you create the DataContext object, you can create the local database and perform a number of additional database operations.
NOTE: When the database is created, it is automatically assigned a version of 0. To determine the database version, use the DatabaseSchemaUpdater class.
For reference you can also take a look at the full MSDN documentation.
Example:
Note that before we can use a local database, it has to exist. This is why, in the code sample below, we check if the database exists and if it doesn't we use the CreateDatabase() method of the DataContext to create it. (NOTE: Make sure that the ConnectionString is right!)
private const string ConnectionString = @"isostore:/CountryDB.sdf";
public MainPage()
{
InitializeComponent();
using (CountryDataContext context = new CountryDataContext(ConnectionString))
{
if (!context.DatabaseExists())
{
// create database if it does not exist
context.CreateDatabase();
}
}
}
Where CountryDataContext is implemented in the following way:
public class CountryDataContext : DataContext
{
public CountryDataContext(string connectionString)
: base(connectionString)
{
}
public Table<Country> Countries
{
get
{
return this.GetTable<Country>();
}
}
public Table<City> Cities
{
get
{
return this.GetTable<City>();
}
}
}
Important Note: In the example above, when calling CreateDatabase(), the database is created in the IsolatedStorage (note the isostore keyword in the connection string). In Windows Phone 7 all applications are "isolated" from each other, which means that one application can access only its own IsolatedStorage. I.e one database can be used only from one application and can not be shared between several applications.
In this article I talked about creating a local database in Windows Phone Mango. Stay tuned for the rest of the posts.
You may also find interesting the following articles:
- Windows Phone Mango Local Database(SQL CE): Introduction
- Using SqlMetal to generate Windows Phone Mango Local Database classes
- A Simple ToDo List: Persisting Data with the Sterling NoSQL Database
- All about WP7 Isolated Storage series
You can also follow us on Twitter @winphonegeek
Comments
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
