设为首页 收藏本站
查看: 359|回复: 0

Windows Phone 7 – Simple database example

[复制链接]

尚未签到

发表于 2015-5-12 09:53:41 | 显示全部楼层 |阅读模式
  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



view source
print?

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



view source
print?

01

02

03   

04    Lewis

05    Franklin

06   

07   

08    Whitcomb

09    Donald

10   

11   

12    Kadi

13    Wadad

14   

15
  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.



view source
print?

01public class StudentList : List

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



view source
print?

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“

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-66151-1-1.html 上篇帖子: windows 7(x86) IE8 下的 owc 下篇帖子: Windows Phone 7<三>触控程序开发
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表