Windows Phone本地数据库(SQLCE):13、更新数据(翻译)
时间:2022-03-10 17:57
这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十三篇。 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知识点。我将谈谈在windows phone mango本地数据库里怎么更新数据。
更新数据到数据库是一个三个步骤的过程。首先,查询要被更新的对象,然后更改数据,最后调用SubmitChanges 方法保存这些更改到数据库。 注释:如果你绑定在DataContext里的对象到页面上的控件,根据用户的交互自动更新数据。然后,在期望的时间里只要一个步骤要求调用SubmitChanges 方法。 注释:直到SubmitChanges 方法被调用数据才会更新。 参考:你可以看看MSDN文档1、怎么更新数据
在开始之前,假设我们有下面两张表的数据库结构:Country和CityDataContext如下
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本地数据库(SQLCE):13、更新数据(翻译),布布扣,bubuko.com