Windows Phone local database (sqlce): 8. datacontext)

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 address: http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database-SQL-CE--DataContext

The text is as follows:

This is the eighth 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 using datacontext in the Windows Phone mango local database.

1. What is datacontext? The purpose of datacontext is to display the database to other codes in an object-oriented manner. Datacontext has three important features: (1) it starts from system. data. LINQ. datacontext inherited class (2) the datacontext constructor must call the base (connectionstring) constructor (base class constructor) (3) datacontext passed the type table <tentity> (for example: table <city>) to display tables in the database. This table class implements iqueriable <tentity> and allows you to write a LINQ query to query the database. 2. How to Create datacontext to create a local database, you must first define datacontext and object class. These classes define the ing between the data object model and the database mode. The object-to-SQL relationship capability depends on the detailed information of the ing. The detailed information of the ing is used to create a relational database to map to the corresponding datacontext. Reference: You can see the msdn document: http://msdn.microsoft.com/zh-cn/library/hh202860 (V = vs.92). aspx example:

 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  }
3. How to Use datacontext to check whether a database exists is very important. If it does not exist, you must create it. Example:
 1 public MainPage() 2  { 3      InitializeComponent(); 4     5      using (CountryDataContext context = new CountryDataContext(ConnectionString)) 6      { 7     8          if (!context.DatabaseExists()) 9          {10              // create database if it does not exist11              context.CreateDatabase();12          }13      }14  } 

Now you have context and the database has been created. You can query it as in the example:

1 var query = from c in context.City where p.Name ="London" select p;

This article discusses datacontext in the Windows Phone mango local database and how to use them. Continue to pay attention to the following articles.

 

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.