This is the 13th article in the "Windows Phone Mango Local Database (SQLCE)" series. 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 update the data in the Windows Phone Mango Local database.
updating data to a database is a three-step process. First, query the object to be updated, then change the data, and finally call the SubmitChanges method to save these changes to the database.
Note : If you bind an object in DataContext to a control on a page, the data is automatically updated based on the user's interaction. Then, in the desired time, only one step is required to call the SubmitChanges method.
Note : The data will not be updated until the SubmitChanges method is called. Reference: You can look at the MSDN documentation http://msdn.microsoft.com/zh-cn/library/hh202861 (v=vs.92). aspx1. How to update databefore we start, let's say we have the database structure of the following two tables: Country and City
DataContext 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}
in the following code example, I will demonstrate several procedures:1. Create DataContext2, find the target to be updated "city"3, update city's name Madrid4. Call the SubmitChanges method to save the changes.
1PrivatevoidUpdatecity ()2{3using (countrydatacontext context =NewCountrydatacontext (ConnectionString))4{5//Find a city to update6 iqueryable<city> cityquery =From cIn context. Citieswhere c.name = ="Barcelona"Select C; Cityquery.firstordefault (); 8 9 // update the city by Changing its name10 citytoupdate.name = "madrid "; One 12 // save changes to The Database13 context. SubmitChanges (); 14 }15}
This article I talked about updating data on the Windows Phone Mango Local database. Keep your eye on the next article.