博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Phone 7.5 - Local SQL Database:Mango Local Database(SQL CE)
阅读量:5752 次
发布时间:2019-06-18

本文共 5344 字,大约阅读时间需要 17 分钟。

DatabaseWindows Phone Mango Local Database(SQL CE) 系列

 

by WindowsPhoneGeek

This is the 13th post from the "Windows Phone Mango Local Database" series of short posts that will cover all you need to know in order  to get started using a Local Database in Windows Phone 7.1 Mango.  This time I am going to talk about how to update data  when working with a Windows Phone 7.1 Mango local database.

Here is what else is included in this series:

Updating data into the database is a three-step process.  First, query the database for the object that is to be updated. Then, modify the object as desired. Finally, call the SubmitChanges method to save the changes to the local database.

NOTE: If you bind objects in the data context to controls on the page, the data context can be updated automatically based on user interaction. Then, the only step required is to call the SubmitChanges method at the desired time.

NOTE: Data is not updated in the database until the SubmitChanges method is called.

For reference you can also take a look at the full .

How to Update Data?

Before we begin lets assume that we have the following database structure with two tables: Country and City:

The DataContext is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public
class
CountryDataContext : DataContext
{
   
public
CountryDataContext(
string
connectionString)
       
:
base
(connectionString)
   
{
   
}
 
   
public
Table<Country> Countries
   
{
       
get
       
{
           
return
this
.GetTable<Country>();
       
}
   
}
 
   
public
Table<City> Cities
   
{
       
get
       
{
           
return
this
.GetTable<City>();
       
}
   
}
}

In the code sample below we will demonstrate the process of:

  • creating the data context
  • finding the target "City" that will be updated
  • updating the Name of the city to "Madrid"
  • finally we will call SubmitChanges() to save the changes back to the database

 

 

private 
void UpdateCity(){    
using (CountryDataContext context = 
new CountryDataContext(ConnectionString))    {        
//
 find a city to update        IQueryable<City> cityQuery = from c in context.Cities where c.Name == "Barcelona" select c;        City cityToUpdate = cityQuery.FirstOrDefault();                  
//
 update the city by changing its name        cityToUpdate.Name = "Madrid";          
//
 save changes to the database        context.SubmitChanges();    }}

 

 

How to insert Data?

Before we begin lets assume that we have the following database structure with two tables: Country and City:

[Table]public class Country{ //...}[Table]public class City{    //...}

The DataContext is as follows;

public class CountryDataContext : DataContext{    public CountryDataContext(string connectionString)        : base(connectionString)    {    }     public Table
Countries { get { return this.GetTable
(); } } public Table
Cities { get { return this.GetTable
(); } }}

In the code sample below we will demonstrate the process explained above be crating and inserting two new related objects in the database - a country and a city. First, we create a new country instance and add it to the context (using the InsertOnSubmit method). Next, we create a new city instance, assign the country instance that we just create, and add it to the context. Finally we call the SubmitChanges method in order to actually save the changes to the database.

private void AddCity(){    using (CountryDataContext context = new CountryDataContext(ConnectionString))    {        // create a new country instance        Country country = new Country();        country.Name = "Spain";        // add the new country to the context        context.Countries.InsertOnSubmit(country);        // create a new city instance        City city = new City();        city.Name = "Barcelona";        // assing country        city.Country = country;        // add the new city to the context        context.Cities.InsertOnSubmit(city);        // save changes to the database        context.SubmitChanges();    }}

How to Delete Data?

Before we begin, lets assume that we have the following database structure with two tables: Country and City:

The DataContext is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public
class
CountryDataContext : DataContext
{
   
public
CountryDataContext(
string
connectionString)
       
:
base
(connectionString)
   
{
   
}
  
   
public
Table<Country> Countries
   
{
       
get
       
{
           
return
this
.GetTable<Country>();
       
}
   
}
  
   
public
Table<City> Cities
   
{
       
get
       
{
           
return
this
.GetTable<City>();
       
}
   
}
}

In the code sample below we will demonstrate the process of:

  • creating the data context
  • finding the target "City" that will be delete
  • deleting the City from the data context
  • finally we will call SubmitChanges() to save the changes back to the database
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private
void
DeleteCity()
{
   
using
(CountryDataContext context =
new
CountryDataContext(ConnectionString))
   
{
       
// find a city to delete
       
IQueryable<City> cityQuery = from c
in
context.Cities where c.Name ==
"Madrid"
select c;
       
City cityToDelete = cityQuery.FirstOrDefault();
        
       
// delete city from the context
       
context.Cities.DeleteOnSubmit(cityToDelete);
       
// save changes to the database
       
context.SubmitChanges();
   
}
}

In this article I talked about inserting data when working with a Windows Phone 7.1 Mango local database. Stay tuned for the rest of the posts.

You may also find interesting the following articles:

转载地址:http://ntkkx.baihongyu.com/

你可能感兴趣的文章
算法试题 - 找出最小 k 个数
查看>>
HDU 2544 最短路
查看>>
MyBatis 逆向工程
查看>>
iOS单例
查看>>
webpack 1.x 配合npm scripts管理多站点
查看>>
windows下Emacs的安装与配置
查看>>
CF Watto and Mechanism (字典树+深搜)
查看>>
【转】jQuery教程
查看>>
关于markdown怎么在博客园展示出来的问题
查看>>
bootstrap中的 form表单属性role="form"有什么作用?
查看>>
c#日期与字符串间的转换(转)
查看>>
Java基础(四)
查看>>
移动端禁止登陆
查看>>
Steve Jobs 2005年于 Stanford University 毕业典礼上的演讲
查看>>
java中枚举类的实际应用
查看>>
课程设计__C++初步,C++对C的扩充
查看>>
Codeforces 758A Holiday Of Equality
查看>>
P25、面试题1:赋值运算符函数
查看>>
Ajax详细介绍
查看>>
前端页面优化技巧
查看>>