Windows Phone local database (sqlce): 10. Create a database)

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 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 creating a Windows Phone mango local database.

1. Create a database

After you create a datacontext object, you can create a local database and perform some additional database operations.

Note: After a database is created, it is automatically assigned a version. To determine the database version, use the databaseschemaupdater class. Reference: You can take a look at the msdn documentation: http://msdn.microsoft.com/zh-cn/library/hh202861 (V = vs.92). aspx example: Note: Before you start using the local database, it must exist. This is why we need to check whether the database exists in the following code. If it does not exist, we need to use the createdatabase () method of datacontext to create a database. (Note that the connection string must be correct)
 1 private const string ConnectionString = @"isostore:/CountryDB.sdf"; 2     3  public MainPage() 4  { 5      InitializeComponent(); 6     7      using (CountryDataContext context = new CountryDataContext(ConnectionString)) 8      { 9    10          if (!context.DatabaseExists())11          {12              // create database if it does not exist13              context.CreateDatabase();14          }15      }16  }

Countrydatacontext:

 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  }
Important Notes: In the preceding example, when createdatabase () is called, the database is created in isolatedstorage (note the isostore keyword in the connection string ). All applications in widows Phone 7 are isolated from each other, which means that a program can only access its own isolatedstorage, that is, a database can only be used by one application and cannot be shared among multiple applications. This article describes how to create a local database in Windows Phone mango. Continue to pay attention to the following articles.



This is the original address: http://windowsphonegeek.com/tips/Windows-Phone-Mango-Local-Database-SQL-CE--Creating-the-Database

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.