http://rongchaua.net/blog/windows-phone-7-simple-database-example/
Today when I start to play around with developing on Windows Phone 7, I would like to write a first small database application because Windows Phone does not support SQL Server any more. Therefore I must use either LINQ over XML or store the database somewhere on server/Windows Azure and provide services so that the client can access and make query to get and update data. In this small example, I would like to work with LINQ To XML to create a database and update it.
My data object (data entity) is a class of Student with his first name, last name and email built by appending first name and last name as describing below
03 |
public string EMail { get ; set ; } |
04 |
public string FirstName { get ; set ; } |
05 |
public string LastName { get ; set ; } |
07 |
public Student( string FirstName, string LastName) |
09 |
this .FirstName = FirstName; |
10 |
this .LastName = LastName; |
11 |
EMail = FirstName + "@" + LastName + ".com" ; |
14 |
public Student(XElement xElement) |
16 |
EMail = xElement.Attribute( "EMail" ).Value; |
17 |
FirstName = xElement.Element( "FirstName" ).Value; |
18 |
LastName = xElement.Element( "LastName" ).Value; |
21 |
public XElement Information |
25 |
return new XElement( "Student" , |
26 |
new XAttribute( "EMail" , EMail), |
27 |
new XElement( "FirstName" , FirstName), |
28 |
new XElement( "LastName" , LastName)); |
Then I create a sample database with some predefined students
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
03 |
< Student EMail = "Lewis@Franklin.com" > |
04 |
< FirstName >Lewis</ FirstName > |
05 |
< LastName >Franklin</ LastName > |
07 |
< Student EMail = "Whitcomb@Donald.com" > |
08 |
< FirstName >Whitcomb</ FirstName > |
09 |
< LastName >Donald</ LastName > |
11 |
< Student EMail = "Kadi@Wadad.com" > |
12 |
< FirstName >Kadi</ FirstName > |
13 |
< LastName >Wadad</ LastName > |
This database will be loaded into a list of student. Everytime when I add a new student, the new one will be added in this list and the list will be flushed back into XML database. However the predefined database locates outside the management field of application therefore we have no right to write data back. To write data back to XML file, I must store it in isolated storage of application and work with this replication.
01 |
public class StudentList : List<Student> |
03 |
public void Load( string strXMLFile) |
05 |
IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication(); |
07 |
IsolatedStorageFileStream isfStream = null ; |
08 |
if (isfData.FileExists(strXMLFile)) |
10 |
isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData); |
11 |
doc = XDocument.Load(isfStream); |
16 |
doc = XDocument.Load(strXMLFile); |
17 |
isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData); |
22 |
var vStudent = from s in doc.Descendants( "Student" ) |
23 |
select new Student(s); |
28 |
public void Save( string strXMLFile) |
32 |
XElement xml = new XElement( "Students" , |
34 |
select p.Information); |
36 |
IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication()); |
42 |
MessageBox.Show(ex.Message); |
To work with the database we just need to initialize an object of StudentList and call its methods to manipulate data
1 |
private void LoadDatabase() |
3 |
m_dbList = new StudentList(); |
4 |
m_dbList.Load( "Database.xml" ); |
5 |
lvData.ItemsSource = m_dbList; |
The complete source code of this example you can download here “Windows Phone Database Example“