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.