This is the eighth article in the "Windows Phone Mango Local Database (SQLCE)" series. To get you started using the database in Windows Phone Mango, this series of short film articles will cover all the knowledge you need to know. I'll talk about using connection strings in the Windows Phone Mango Local database.
1. What is connectionstrings?before we actually start using a database, we need to develop a connection string that tells the application how to connect to the database. A connection string can be used to make a database configuration value. In the connection string, each parameter is separated by a semicolon, and the value of the parameter is enclosed in quotation marks. Some parameters apply only to creating a database, and after the database is created, these parameters are ignored. The connection string for a particular format should look like this:
"Data source= ' isostore:/directory/file.sdf '";
Reference : You can look at the MSDN documentation http://msdn.microsoft.com/zh-cn/library/hh202861 (v=vs.92). aspx2. How to use connectionstringsExample 1: Use of a parameterString Format:
"Data source= ' isostore:/directory/file.sdf '";
Note :
isostore represents a path to IsolatedStorage?
12345678910111213141516 |
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();
}
}
}
|
Example 2: Read String format:
"Data source= ' appdata:/directory/file.sdf '" from the installation folder;
Note : AppData indicates the path to the installation folder ?
123456789101112131415 |
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();
}
}
}
|
Example 3: A database with a specific culture
?
1 |
private const string ConnectionString = @"Data Source = ‘CountryDB.sdf‘; Culture Identifier = fr-FR; Case Sensitive = true;" ; |
Note : You can refer to the MSDN documentation: Http://msdn.microsoft.com/zh-cn/library/system.globalization.cultureinfo (v=vs.71). aspx
Example 4: Database encryption
String format: "Data source= ' isostore:/dirctory/file.sdf '; password= ' Somepassword ' "
?
1 |
private const string ConnectionString = @"Data Source=‘isostore:/CountryDB.sdf‘;Password=‘MyPassword‘;" ; |
This article I talked about the connection string in the Windows Phone Mango Local database and how to use it. Keep your eye on the next article.