Windows Phone Mango Local Database(SQL CE): Connection Strings
published on: 9/7/2011 | Views: N/A | Tags: LocalDB Mango
by WindowsPhoneGeek
This is the 9th 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 "Connection Strings" when working with a Local Database in Windows Phone 7.1 Mango.
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
What is Connection String?
Before we can actually use a database at all, we need to specify a connection string, which basically tells the application how to connect to the database. A connection string can be used to specify database configuration values. In a connection string, individual parameters are separated by semicolons and parameter values are placed within single quotes. Some parameters are applicable only to creating the database; after the database has been created, those parameters are ignored.
A special format of the connection string must be used like for example:
"Data Source='isostore:/DIRECTORY/FILE.sdf'";
For reference you can also take a look at the full MSDN documentation.
How to use a connection string?
Example1. Single parameter usage
String format: "Data Source='isostore:/DIRECTORY/FILE.sdf'";
NOTE: isostore indicates that the path points to isolated storage.
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();
}
}
}
Example2. Reading from the Installation Folder
String format: "Data Source='appdata:/DIRECTORY/FILE.sdf'";
NOTE: appdata indicates that the path points to the installation folder.
private const string ConnectionString = @"Data Source = 'appdata:/CountryDB.sdf'; File Mode = read only;";
public MainPage()
{
InitializeComponent();
using (CountryDataContext context = new CountryDataContext(ConnectionString))
{
if (!context.DatabaseExists())
{
// create database if it does not exist
context.CreateDatabase();
}
}
}
Example3. Database with Specific Culture
private const string ConnectionString = @"Data Source = CountryDB.sdf'; Culture Identifier = fr-FR; Case Sensitive = true;";
NOTE: For reference about the supported Cultures take a look at the MSDN documentation.
Example4. Encrypted Database
String format: "Data Source='isostore:/DIRCTORY/FILE.sdf';Password='SomePassword'"
private const string ConnectionString = @"Data Source='isostore:/CountryDB.sdf';Password='MyPassword';";
In this article I talked about what is a connection string and how to use it when working with a Windows Phone Mango Local Database. 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
