Windows Phone local database (sqlce): 12. insert data)

Source: Internet
Author: User

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 address: Ghost. This is the first 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 insert data in the Windows Phone mango local database.

Inserting data into the database is a two-step process. First, use the insertonsubmit method to add an object to datacontext, and then call the submitchanges method of datacontext to save the data as rows in the database. Note: Data is not actually saved to the database until submitchanges calls. 1. How to insert data before starting, suppose we have the database structure of the following two tables: country and city

 1 [Table] 2  public class Country 3  { 4   //... 5  } 6    7    8  [Table] 9  public class City10  {11      //...12  }

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  }

In the following code example, we will demonstrate this process, explain the above boxed and insert two new relational objects (city and country) into the database. First, create a new country instance and add it to the context (using the insertonsubmit method ). Next, create a city instance, assign it to the country instance we just created, and add it to context. Finally, we call the submitchanges method to save these changes to the database.

 1 private void AddCity() 2  { 3      using (CountryDataContext context = new CountryDataContext(ConnectionString)) 4      { 5          // create a new country instance 6          Country country = new Country(); 7          country.Name = "Spain"; 8          // add the new country to the context 9          context.Countries.InsertOnSubmit(country);10   11          // create a new city instance12          City city = new City();13          city.Name = "Barcelona";14          // assing country15          city.Country = country;16          // add the new city to the context17          context.Cities.InsertOnSubmit(city);18   19          // save changes to the database20          context.SubmitChanges();21      }22  }

This article describes how to insert data into a Windows Phone mango local database. Continue to pay attention to the following articles.

 

 

This is the original address: http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database-SQL-CE--How-to-Insert-data
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.