DatabaseWindows Phone Mango Local Database(SQL CE) 系列
http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database-SQL-CE--How-to-Delete-data
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:
- Windows Phone Mango Local Database(SQL CE): Introduction
- Windows Phone Mango Local Database(SQL CE): Linq to SQL
- Windows Phone Mango Local Database(SQL CE): [Table] attribute
- Windows Phone Mango Local Database(SQL CE): [Column] attribute
- Windows Phone Mango Local Database(SQL CE): [Association] attribute
- Windows Phone Mango Local Database(SQL CE): [Index] attribute
- Windows Phone Mango Local Database(SQL CE): Database mapping
- Windows Phone Mango Local Database(SQL CE): DataContext
- Windows Phone Mango Local Database(SQL CE): Connection Strings
- Windows Phone Mango Local Database(SQL CE): Creating the Database
- Windows Phone Mango Local Database(SQL CE): Database Queries with LINQ
- Windows Phone Mango Local Database(SQL CE): How to Insert data
- Windows Phone Mango Local Database(SQL CE): How to Update data
- Windows Phone Mango Local Database(SQL CE): How to Delete data
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 MSDN documentation.
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
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<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 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(); } }
|