Local Database overview for Windows Phone

Source: Internet
Author: User

In WP7 os7.1, data can be stored in a local database that is stored independently.

Use LINQ to SQL to perform all the operations, including defining the database structure, querying data, and saving the changes to the original database file in the independent storage.

 

As shown in the figure, the LINQ to SQL model is used to bridge applications.ProgramAnd Database

Data Context

Data context is a proxy used to represent databases. It contains many tables. Each table is composed of corresponding entity classes.

Similarities and differences (compared with computers)

    • A local database runs in the Windows Phone application's process. Unlike a client-server database such as Microsoft SQL Server, it does not run continuously as a background service.

    • A local database can be accessed only by the corresponding Windows Phone application. Because the database file resides in isolated storage, no other applications can access that data.

    • A local database can be accessed only with LINQ to SQL; Transact-SQL is not supported.

To add an existing database file, follow these steps:

  1. Create the helper application: The helper application runs on your development computer, creates the local database in isolated storage, and loads the database with the desired reference data.

  2. Extract the local database from the helper application: Use the isolated storage explorer (isw.l.exe) to copy the database from the helper application to a folder on your computer. For more information about the isolated storage explorer, seeHow to: Use the isolated storage explorer tool.

  3. Create the primary application: Create the application that will consume the reference data.

  4. Add the reference data to the primary application: Use Visual Studio to add the local database file to the primary application from the folder where you saved it on your computer. to minimize the size of the application's assembly, store the fileContent.

After a local database is deployed with an application, it resides in the installation folder in a read-only state. the installation folder is different than isolated storage. to address the database file in this location, useAppdata:Prefix.

To modify the database containing the reference data, move it out of the installation folder and save it in isolated storage before attempting database changes. to move the database file, you can perform a stream-based copy withApplication. getresourcestreamMethod To create a stream from the installation folder, andIsolatedstoragefilestream. WriteMethod To write the stream to isolated storage. The following example demonstrates how to address a database file in the installation folder when you create a stream object.

 
Stream STR = application. getresourcestream (New uri ("appdata:/myreferencedb. SDF", urikind. Relative). stream;

Use data context to operate databases:

1. First create data context and object class:

Public class tododatacontext: datacontext {// specify the connection string as a static, used in Main Page and app. XAML. public static string dbconnectionstring = "Data Source = isostore:/todo. SDF "; // pass the connection string to the base class. public tododatacontext (string connectionstring): Base (connectionstring) {}// specify a single table for the to-do items. public table <todoitem> todoitem S;} // define the to-do items database table. [Table] public class todoitem: inotifypropertychanged, inotifypropertychanging {// define ID: Private field, public property, and database column. private int _ todoitemid; [column (isprimarykey = true, isdbgenerated = true, dbtype = "int not null identity", canbenull = false, autosync = autosync. oninsert)] public int todoitemid {get {return _ todoitemid;} Set {If (_ todoitemid! = Value) {categorypropertychanging ("todoitemid"); _ todoitemid = value; categorypropertychanged ("todoitemid ");}}}.........

Add reference

 
Using system. Data. LINQ; using system. Data. LINQ. Mapping; using Microsoft. Phone. Data. LINQ; using Microsoft. Phone. Data. LINQ. Mapping;

Create a database:

 
// Create the database if it does not yet exist. using (tododatacontext DB = new tododatacontext ("isostore:/todo. SDF ") {If (dB. databaseexists () = false) {// create the database. DB. createdatabase ();}}

For more information, see:

Http://msdn.microsoft.com/en-us/library/hh202860 (V = vs.92). aspx

Source code: http://msdn.microsoft.com/en-us/library/ff431744.aspx

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.