Windows Phone本地数据库(SQLCE):13、更新数据

    一只大菜鸟,最近要学习windows phone数据库相关的知识,找到了一些比较简短的教程进行学习,由于是英文的,顺便给翻译了。本身英语水平就不好,估计文中有不少错误,如果有不幸读到的童鞋请保持对翻译质量的质疑,多多指教。

      这是原文地址:http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database-SQL-CE--How-to-Update-data
正文如下:
    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十三篇。 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知识点。我将谈谈在windows phone mango本地数据库里怎么更新数据。
 
    更新数据到数据库是一个三个步骤的过程。首先,查询要被更新的对象,然后更改数据,最后调用SubmitChanges 方法保存这些更改到数据库。
 
注释:如果你绑定在DataContext里的对象到页面上的控件,根据用户的交互自动更新数据。然后,在期望的时间里只要一个步骤要求调用SubmitChanges 方法。
注释:直到SubmitChanges 方法被调用数据才会更新。
 
 

1、怎么更新数据

在开始之前,假设我们有下面两张表的数据库结构:Country和City

Windows Phone本地数据库(SQLCE):13、更新数据

DataContext如下

 1 public class CountryDataContext : DataContext

 2  {

 3      public CountryDataContext(string connectionString)

 4          : base(connectionString)

 5      {

 6      }

 7     

 8      public Table<Country> Countries

 9      {

10          get

11          {

12              return this.GetTable<Country>();

13          }

14      }

15     

16      public Table<City> Cities

17      {

18          get

19          {

20              return this.GetTable<City>();

21          }

22      }

23  }
下面的代码示例中我将演示几个过程:
1、创建DataContext
2、找到要被更新的目标“City”
3、更新City的名字Madrid
4、调用SubmitChanges方法保存更改。
 1 private void UpdateCity()

 2  {

 3      using (CountryDataContext context = new CountryDataContext(ConnectionString))

 4      {

 5          // find a city to update

 6          IQueryable<City> cityQuery = from c in context.Cities where c.Name == "Barcelona" select c;

 7          City cityToUpdate = cityQuery.FirstOrDefault();

 8            

 9          // update the city by changing its name

10          cityToUpdate.Name = "Madrid";

11    

12          // save changes to the database

13          context.SubmitChanges();

14      }

15  }

    这篇文章我谈论了在windows phone mango本地数据库更新数据。请继续关注接下来的文章。

 


你可能感兴趣的:(windows phone)