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


     这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十三篇。 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知识点。我将谈谈在windows phone mango本地数据库里怎么更新数据。

     
        更新数据到数据库是一个三个步骤的过程。首先,查询要被更新的对象,然后更改数据,最后调用SubmitChanges 方法保存这些更改到数据库。
     
    注释:如果你绑定在DataContext里的对象到页面上的控件,根据用户的交互自动更新数据。然后,在期望的时间里只要一个步骤要求调用SubmitChanges 方法。
    注释:直到SubmitChanges 方法被调用数据才会更新。
     
     

    1、怎么更新数据

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

    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本地数据库更新数据。请继续关注接下来的文章。

  • 相关阅读:
    Yii调试插件yii-debug-toolbar的使用
    IE8不支持indexOf的解决办法
    使用wkhtmltopdf的一些事情
    mac sourcetree 启用 Beyond compare
    Java基础学习之(5)--impact和package
    Java基础学习之(3)--面向对象2--重载
    Java基础学习之(4)--面向对象3--this+static关键字
    Java基础学习之(2)--面向对象1
    Java基础学习之(1)--标识符、关键字、数据类型
    java学习(7)iterator迭代器
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/3838294.html
Copyright © 2020-2023  润新知