Last three days we learnt about the new feature of Windows Phone 7 (Mango) called "Local Database Support". Yesterday we discussed it in depth by building a small demo application. While working with the demo you might noticed that, we used a connection string to connect with the local database present in the Windows Phone storage.

 

In this post, we will discuss more on the connection string. After reading this post, you will be able to know what are the various types and parameters available for connection string.

 

Index - Windows Phone 7 (Mango) Tutorial

 

What is Connection String?

A connection string is a settings to connect with the database file using a Windows Phone 7 application. Using this string parameter you ask your app to connect to an existing database or create a new one to do SQL operations.

 

By using the connection string you can specify the connection parameters like data source, password, buffer size, database size, file mode, culture etc. You can specify some or all parameters by separating them using a ( ; ) semicolon. All parameters are not always require as some of them use to create the database.

 

Connection Parameters

There are various connection parameters available to connect to the database. Let's discuss some of the parameters here.

In our last chapter of the tutorial series we used "isostore:/Northwind.sdf" as the connection string. What does it mean? It tells the data context to connect with the database named "Northwind.sdf" present in the isolated storage. You can change the location of the database to application installed directory by replacing the "isostore" with "appdata".

Data Source

You can specify the DataSource of the connection string either using isostore or appdata. While creating the data context object, you can use "Data Source" property to specify the value. If you don't use the property name, by default it takes it as data source.

 
string ConnectionString = @"isostore:/Northwind.sdf";
string ConnectionString = @"appdata:/Northwind.sdf"; 
 
string ConnectionString = @"DataSource = 'isostore:/Northwind.sdf';";
string ConnectionString = @"DataSource = 'appdata:/Northwind.sdf';";

 

Password

The database password is used to encrypt the database at the time of creation. Later you have to use it to connect to the database. A database character can be of maximum 40 characters length and if not specified, will treat it as unencrypted database.

 
string ConnectionString = @"Data Source = 'isostore:/Northwind.sdf'; Password = 'mypassword';";

 

File Mode

File Mode specifies how the database file will open. There are four different values named as "Read Write", "Read Only", "Exculsive" and "Shared Read".

 
// Allows multiple processes  to read and write to the database. This is the default value.
string ConnectionString = @"Data Store = 'isostore:/Northwind.sdf'; File Mode = 'Read Write';";
 
// Allows process to open the database in Read only mode.
string ConnectionString = @"Data Store = 'isostore:/Northwind.sdf'; File Mode = 'Read Only';";
 
// Allows a single process to read and write to the database.
string ConnectionString = @"Data Store = 'isostore:/Northwind.sdf'; File Mode = 'Exclusive';";
 
// Allows all processes to read the database but only one process can write into it.
string ConnectionString = @"Data Store = 'isostore:/Northwind.sdf'; File Mode = 'Shared Read';";

 

Maximum Database Size

Max Database Size property specifies the maximum size of the database in MB. The size ranges from 32MB - 512MB.

 
string ConnectionString = @"Data Store = 'isostore:/Northwind.sdf'; Max Database Size = 128;";

 

Maximum Buffer Size

Max Buffer Size property specifies the maximum buffer memory for the database in KB. It ranges from 384KB - 5120KB.

 
string ConnectionString = @"Data Store = 'isostore:/Northwind.sdf'; Max Buffer Size = 2048;";

 

Culture

Culture of the database can be set using the Culture Identifier property. It accepts culture value as "language-Country" format. For example, "en-US" specifies the default culture English for United States.

 
string ConnectionString = @"Data Store = 'isostore:/Northwind.sdf'; Culture Identifier = en-US;";

 

Case Sensitive Database

You can specify whether a database will be case sensitive or not by using the Case Sensitive property. A boolean value defines whether the database will be case sensitive.

 
string ConnectionString = @"Data Store = 'isostore:/Northwind.sdf'; Case Sensitive = true;";

 

 

Hope this information will be helpful for you to understand the connection string composition to connect to a database in Windows Phone 7 (Mango) applications. Try the same with the existing application that we created in last post. Thanks for your time to read my blog.

 

Don't forget to share the links to your friends to reach out the content to maximum people. Follow me on Twitter @kunal2383. I also have my blog page on Facebook. Connect to be updated on latest posts and information updates by clicking the "Like" button in my Facebook page.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.