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

01public 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.

01public 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

1private 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

转载于:https://www.cnblogs.com/nio-nio/archive/2010/09/18/1830491.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值