Professional Documents
Culture Documents
W I N D O W S P H O N E
21 76
In the previous article, you learned how to store data in your app's isolated storage. In
this article, we focus on working with local databases that live in your app's isolated
storage. You'll learn about database operations using LINQ, database schemas as well
as how to fetch data from a local database.
1. Introduction
You can store relational data in a local database that lives in your app's isolated
storage. All database operations on Windows Phone are performed using LINQ to
SQL. It is used to define the database schema, select data, and save changes to the
underlying database file residing in the local folder.
When working with local databases on Windows Phone, it's important to keep the
following in mind:
The local database runs in the Windows Phone app's process. It does not run
continuously as a background service.
It can be accessed only by the corresponding Windows Phone app.
It can be accessed only with LINQ to SQL, Transact-SQL is not supported.
To synchronize access to the local folder across different threads, the Mutex
class is used.
It's not recommended to encrypt the reference database file if you're going to
access it exclusively from the installation folder. Doing so prevents the system
from performing routine database maintenance operations, such as re-indexing,
upon the first connection.
This article shows how you can create a local database and insert, update or delete
data from it. We will build a sample application with a User Details table and perform
various operations on it. We will segregate the code files for different operations such
as insert, update and delete for the sake of convenience.
converted by Web2PDFConvert.com
specify an object model that determines the database schema and create the data
context.
1 using System.Data.Linq;
2 using System.Data.Linq.Mapping;
01 [Table]
02 public class User_details
03 {
04 [Column(IsDbGenerated = true, IsPrimaryKey = true)]
05 public int ID
06 {
07 get;
08 set;
09 }
10 [Column]
11 public string user_name
12 {
13 get;
14 set;
15 }
16 [Column]
17 public string user_email
18 {
19 get;
20 set;
21 }
22 }
As you can see, the User_details class has three public properties that correspond to
three database columns:
converted by Web2PDFConvert.com
01 public class UserDataContext : DataContext
02 {
03 public static string DBConnectionString = @"isostore:/Databases.sdf";
04 public UserDataContext(string connectionString)
05 : base(connectionString)
06 {}
07 public Table<User_details> Users
08 {
09 get
10 {
11 return this.GetTable<User_details>();
12 }
13 }
14 }
Note that the database connection string is not required to be a static field. We
use DBConnectionString in this case just for convenience.
The database creation code is added here so that the database will be present before
the code from the main page runs. The code checks whether the database exists and, if
no database is found, a new one is created.
4. Database Operations
Once the data context has been built and the database has been created, we can
perform operations on it. You can insert, update, and delete records from the table we
just created. We have created separate classes to hold functions for insert, delete, and
update operations.
The AddUser function uses the UserDataContext data context to connect to the
converted by Web2PDFConvert.com
database, creates a new instance of the User_details entity class, and inserts a record.
New items that have been added to the data context are not saved to the database until
the SubmitChanges function is called. Call the AddUser function to add a record to the
database.
The connection is set up using the data context after which records are fetched from
the User_details table. The LINQ query returns an IList instance containing the fetched
records. The IList instance is nothing more than a collection of objects of same type.
In the below code snippet we define a Users class with data members id , name , and
email to hold the details from the fetched records.
1 class Users
2 {
3 public string id { get; set; }
4 public string name { get; set; }
5 public string email { get; set; }
6 }
Create another function, getAllUsers , that returns a list of Users when called. The
function creates a new list to hold the details of the fetched users. It iterates through the
IList instance named usrs and adds the details of each user to the allUsers instance.
In the sample application of this article, the getUsers function is used to set the
ItemSource of the Listbox named allusers as shown below.
converted by Web2PDFConvert.com
1 FetchDatabase fetch = new FetchDatabase();
2 allusers.ItemsSource = fetch.getUsers();
Add a function UpdateUsers that accepts an id , name , and email_id , and updates the
table row for the corresponding id .
The function queries the database and stores the first matched record in the
entityToUpdate variable. It then updates the record by assigning the new values and
submits the changes to update the database.
The database is not updated until we call the SubmitChanges function. This function will
update only the first matched record. The UpdateUser function is called to update
existing records in the database as shown below.
To update multiple records, you'll need to iterate through the records that you'd like to
update one by one. In the following code snippet, we update the name of every use in
the database by making it lowercase.
converted by Web2PDFConvert.com
Advertisement
The function calls DeleteOnSubmit , which deletes a single record from the database.
The changes are saved when the SubmitChanges function is called.
The DeleteUser function deletes a single record from the database as shown below.
This function accepts a list of records and deletes the records in that list. Note that in
both cases the changes are saved only when the SubmitChanges function is called.
Conclusion
Windows Phone apps use LINQ to SQL for all database operations. LINQ to SQL is
used to define the database schema, select data, and, save changes to the underlying
database file residing in the app's isolated storage. LINQ to SQL provides an object-
oriented approach for working with data stored in a database and consists of an object
model and a runtime. I recommend you read this MSDN article for best practices on
using local databases. Feel free to download the tutorial's source files to use as
reference.
converted by Web2PDFConvert.com
Advertisement
Difficulty:
Beginner
Length:
Medium
Categories:
Windows Phone Windows Phone SDK C# Mobile Development Visual Studio IDEs
Translations Available:
Tuts+ tutorials are translated by our community members. If you'd like to translate this post into another language, let us know!
View on Github
Advertisement
converted by Web2PDFConvert.com
Windows Phone 8 Development $15
Related Tutorials
Building Your Startup With PHP: Feature Requirements and Database Design
Code
Introduction to Tablesorter
Code
Jobs
Web Producer
at The Sovereign Society in Delray Beach, FL, USA
converted by Web2PDFConvert.com
Chantilly, VA Residents Are `Rattled` By New
Website
Advertisement
Follow Us
Email Newsletters
Get Tuts+ updates, news, surveys &
offers.
Email Address
Subscribe
Privacy Policy
Custom digital services like logo design, WordPress installation, video production and more.
Check out Envato Studio
Build anything from social networks to file upload systems. Build faster with pre-coded PHP scripts.
Browse PHP on CodeCanyon
2015 Envato Pty Ltd. Trademarks and brands are the property of their respective owners.
converted by Web2PDFConvert.com