Windows Phone Mango Local Database(SQL CE): How to Update data
published on: 10/27/2011 | Views: N/A | Tags: LocalDB
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:
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();
}
}
In this article I talked about updating 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:
- Windows Phone Mango Local Database(SQL CE): Introduction
- Using SqlMetal to generate Windows Phone Mango Local Database classes
- A Simple ToDo List: Persisting Data with the Sterling NoSQL Database
- All about WP7 Isolated Storage series
You can also follow us on Twitter @winphonegeek
Comments
How do you update multiple rows
posted by: Eric Anderson on 12/17/2011 7:40:55 PM
This is a great example. However, it only seems to update one row where the criteria are met. What if I have multiple rows that meet that criteria that I want to have updated. I'm having a hard time finding an example for that situation.
Thanks for the help! - Eric-
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
