Windows Phone 7 – Simple database example

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

01 public class Student
02 {
03     public string EMail { get; set; }
04     public string FirstName { get; set; }
05     public string LastName { get; set; }
06   
07     public Student(string FirstName, string LastName)
08     {
09         this.FirstName = FirstName;
10         this.LastName = LastName;
11         EMail = FirstName + "@" + LastName + ".com";
12     }
13   
14     public Student(XElement xElement)
15     {
16         EMail = xElement.Attribute("EMail").Value;
17         FirstName = xElement.Element("FirstName").Value;
18         LastName = xElement.Element("LastName").Value;
19     }
20   
21     public XElement Information
22     {
23         get
24         {
25             return new XElement("Student",
26                     new XAttribute("EMail", EMail),
27                     new XElement("FirstName", FirstName),
28                     new XElement("LastName", LastName));
29         }
30     }
31 }

Then I create a sample database with some predefined students

01 <?xml version="1.0" encoding="utf-8" ?>
02 <Students>
03   <Student EMail="Lewis@Franklin.com">
04     <FirstName>Lewis</FirstName>
05     <LastName>Franklin</LastName>
06   </Student>
07   <Student EMail="Whitcomb@Donald.com">
08     <FirstName>Whitcomb</FirstName>
09     <LastName>Donald</LastName>
10   </Student>
11   <Student EMail="Kadi@Wadad.com">
12     <FirstName>Kadi</FirstName>
13     <LastName>Wadad</LastName>
14   </Student>
15 </Students>

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>
02 {
03     public void Load(string strXMLFile)
04     {
05         IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
06         XDocument doc = null;
07         IsolatedStorageFileStream isfStream = null;
08         if (isfData.FileExists(strXMLFile))
09         {
10             isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData);
11             doc = XDocument.Load(isfStream);
12             isfStream.Close();
13         }
14         else
15         {
16             doc = XDocument.Load(strXMLFile);
17             isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData);
18             doc.Save(isfStream);
19             isfStream.Close();
20         }
21   
22         var vStudent = from s in doc.Descendants("Student")
23                        select new Student(s);
24         this.Clear();
25         AddRange(vStudent);
26     }
27   
28     public void Save(string strXMLFile)
29     {
30         try
31         {
32             XElement xml = new XElement("Students",
33                             from p in this
34                             select p.Information);
35   
36             IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication());
37             xml.Save(isfStream);
38             isfStream.Close();
39         }
40         catch (Exception ex)
41         {
42             MessageBox.Show(ex.Message);
43         }
44     }
45 }

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()
2 {
3     m_dbList = new StudentList();
4     m_dbList.Load("Database.xml");
5     lvData.ItemsSource = m_dbList;
6 }

The complete source code of this example you can download here “Windows Phone Database Example

posted @ 2010-09-18 23:41  大厨无盐煮  阅读(409)  评论(0编辑  收藏  举报