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