Windows phone Local Database (SQLCE): 10, creating a database (translation)

Source: Internet
Author: User

This is the tenth 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 creating a Windows Phone Mango local database.

1. Create a database

After you have created the DataContext object, you can create a local database and perform some additional database operations.

Note : Once a database has been created, it is automatically assigned a version. In order 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). aspxExample: Note : it must exist before you start using the local database. That's why in the code below we're going to check if the database exists, and if it doesn't, we're going to use DataContext's CreateDatabase () method to create the database. (Note that the connection string is correct)
1PrivateConstString ConnectionString =@"Isostore:/countrydb.sdf";23PublicMainPage ()4{5InitializeComponent ();67using (Countrydatacontext context = new Countrydatacontext (ConnectionString))  { 9 10 if (! Context. Databaseexists ())  { // CREATE DATABASE if it does not exist13  context. CreateDatabase (); 14 }15 }16}            
the Countrydatacontext is implemented in the following way
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}                 
Important Note : In the example above, 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, meaning that a program can only access its own isolatedstorage, which means that a database can only be used by one application and cannot be shared among multiple applications. This article I talked about creating a local database on Windows Phone Mango. Keep your eye on the next article.
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.