A newbie recently wants to learn about the Windows Phone database and find some short tutorials. Because it is in English, it is translated by the way. The English level is not good. It is estimated that there are many mistakes in the text. If you have any children's shoes that are unfortunately read, please keep in doubt about the translation quality and give me more advice.
This is the original article: Ghost, the last article in the "Windows Phone mango local database (sqlce)" series. To get you started using databases in Windows Phone mango, this series of short clips will cover all the things you need to know. I will talk about how to delete data in the Windows Phone mango local database.
Deleting data from a database is a three-step process. First, query the objects to be deleted from the database. Then, call the deleteonsubmit method or the deleteallonsubmit method to delete the objects based on one or more objects to be deleted, finally, call the submitchanges method to save the changes to the local database.
Note: Data will not be deleted until the submitchanges method is called. Reference: You can take a look at the msdn documentation: http://msdn.microsoft.com/zh-cn/library/hh202860 (V = vs.92). aspx 1. How to delete data before getting started, suppose we have the database structure of the following two tables: country and city
Datacontext is as follows:
1 public class CountryDataContext : DataContext 2 { 3 public CountryDataContext(string connectionString) 4 : base(connectionString) 5 { 6 } 7 8 public Table<Country> Countries 9 {10 get11 {12 return this.GetTable<Country>();13 }14 }15 16 public Table<City> Cities17 {18 get19 {20 return this.GetTable<City>();21 }22 }23 }
The following code example demonstrates several processes: 1. Create datacontext2, find the target "city" to be deleted; 3. Delete city4 from datacontext; and call the submitchanges method to save the changes.
1 private void DeleteCity() 2 { 3 using (CountryDataContext context = new CountryDataContext(ConnectionString)) 4 { 5 // find a city to delete 6 IQueryable<City> cityQuery = from c in context.Cities where c.Name == "Madrid" select c; 7 City cityToDelete = cityQuery.FirstOrDefault(); 8 9 // delete city from the context10 context.Cities.DeleteOnSubmit(cityToDelete);11 12 // save changes to the database13 context.SubmitChanges();14 }15 }
In this article, I talked about how to delete data in the Windows Phone mango local database. Hope you like them and find useful things.
This is the original address: http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database-SQL-CE--How-to-Delete-data