This is the 14th installment of the "Windows Phone Mango Local Database (SQLCE)" series short story. To get you started using the database in Windows Phone Mango, this series of short film articles will cover all the knowledge you need to know. I'll 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 object to be deleted from the database, and then, depending on the object or objects you want to delete, call the DeleteOnSubmit method or the Deleteallonsubmit method to delete the objects, respectively, so that they are in the delete state, and finally call SubmitChanges method to save changes to the local database.
Note : Data will not be deleted until the SubmitChanges method is called. Reference: You can look at the MSDN documentation: http://msdn.microsoft.com/zh-cn/library/hh202860 (v=vs.92). aspx1. How to delete databefore we start, let's say we have the database structure of the following two tables: Country and City
The DataContext is as follows:
1PublicClassCountrydatacontext:datacontext2{3Public Countrydatacontext (StringconnectionString)4:Base(connectionString)5{6}78Public table<country>Countries9{10Get11{12ReturnThis. Gettable<country>();13 }14 }15 16 public Table <city> Cities17 {18 get19 {20 return this. Gettable<city> (); }22 }23}
The following code example demonstrates several procedures:1. Create DataContext2. Find the target "city" to be deleted3. Delete City from DataContext4. Call the SubmitChanges method to save the changes
1PrivatevoidDeletecity ()2{3using (countrydatacontext context =NewCountrydatacontext (ConnectionString))4{5//Find a city to delete6 iqueryable<city> cityquery =From cIn context. Citieswhere c.name = ="Madrid "select C; 7 city citytodelete = Cityquery.firstordefault (); 8 Span style= "color: #008080;" > 9 // Delete city from the Context10 context. Cities.deleteonsubmit (Citytodelete); 11 12 // save changes to the Database13 context. SubmitChanges (); 14 }15}
This article I talked about deleting data in the Windows Phone Mango Local database. I hope you can enjoy them and find something useful.