Create and obtain physical database files and database references for Windows Phone local databases-reference existing database files

Source: Internet
Author: User

Windows Phone OS 7.1 allows you to store relational data in applicationsProgramThe local database of the independent storage container. The Windows Phone application uses LINQ to SQL to perform all database operations. The LINQ to SQL is used to define the database architecture, select data, and save the changes to the basic database files that reside in the independent storage; will you be excited when it comes to LINQ to SQL? In the window phone, we can persistently store data in this way :), this article will lead you to build the first Windows Phone local database application-contact management.

This series includes the following:

1. Create a database

2. Find the database file from the independent storage and copy it to the PC.

3. Reuse the database in 2 (other programs use the database files in 2 or use the initial data in 2)

 

In the previous article, we copied the physical database files to the PC file directory. The ultimate goal of this article is to reuse or reference the database in other applications, because it already has a physical structure and initialized data, we can call it built-in data here, because other applications can directly read the data in it when referencing this database, of course, you can add, delete, modify, and query data. Okay, let's go.

 

1. First, create a new Windows Phone program in the solution. In the demo, It is mangodatabasetest.

2. Copy the contactordatacontext. CS file to the mangodatabasetest project (including the class contactordatacontext, databasemanager, and other models) because they are responsible for ORMapping and database management;

3. Copy the exported mycontactor. SDF file in the previous article to the mangodatabasetest project. Set the generate attribute to "content", and copy to "always copy"

After these settings, when the local database is deployed, the physical files of the database are stored in the installation folder after the deployment. The installation folder is read-only. The primary application can connect to the database in read-only mode, or copy it to an independent storage for read/write operations. Of course, our demo cannot perform read-only operations on the database, therefore, we need to move the physical database files to the independent storage. Of course, this should be implemented in the program;

Of course, if you want to connect to the database datacontext in read-only mode, you need to connect as follows:

 
Contactordatacontext DB = new contactordatacontext ("Data Source = 'appdata:/resources/mycontactor. SDF '; file mode = read only ;");
 
TIPS:Use the appdata prefix in the file path to distinguish the path in the installation folder (appdata) from the path in the independent storage (isostore). If there is no prefix, the data context applies the path of the independent storage.
 
The demo below uses the method of copying database files to independent storage;

4. Copy physical database files to independent storage

Add the following method to databasemanager:

 ///   <Summary> 
/// Copy database files to independent storage
/// </Summary>
Public Static Void Movereferencedatabase ()
{
// Whether database files already exist in the independent storage
Bool Isexists = databasemanager. exists ();
If (! Isexists) // If no database file exists in the standalone storage, copy it.
{
// Move database files to independent storage to facilitate reading and writing
Isolatedstoragefile ISO = isolatedstoragefile. getuserstoreforapplication ();
Using (Stream Input = application. getresourcestream ( New Uri ( " Resources/mycontactor. SDF " , Urikind. Relative). Stream)
{
Using (Isolatedstoragefilestream output = ISO. createfile ( " Mycontactor. SDF " ))
{
Byte [] Readbuffer = New Byte [ 4096 ];
Int Bytesread =- 1 ;
While (Bytesread = input. Read (readbuffer, 0 , Readbuffer. Length)> 0 )
{
Output. Write (readbuffer, 0 , Bytesread );
}
}
}
}
}

/// <Summary>
/// Whether database files exist
/// </Summary>
/// <Returns> </returns>
Public Static Bool Exists ()
{
Bool Isexists = False ;
Using (Contactordatacontext DB =New Contactordatacontext (dbconnectionstring ))
{
Isexists = dB. databaseexists ();
}
Return Isexists;
}

Call the following in the constructor of APP. XAML. CS:

5. ui can directly copy main. XAML and mainviewmodel can also be copied directly. Currently, the mangodatabasetest project function is the same as mangodatabasedemo, but mangodatabasetest uses the referenced physical database file mycontactor. SDF not only has built-in data, but we can also add, delete, modify, and query data ..

Run mangodatabasetest ..

Add data:

Now we have officially completed the reference to the physical database file ..

 

Review: The databases used in this mango mainly include

1> you can use the database options in window phone. You can also create databases, tables, and indexes in Mango that support the database operations of LINQ to SQL. By creating a simple address book and group, the database creation process is verified.

2> Use isetool in the window phoen SDK. EXE is used to manage files and resources in the independent storage of debugging devices (simulators or Test Machines). It has some command characters. The product ID is used to browse the directories in the independent storage of an application, copy the total files stored independently to the PC file directory.

3> describes how to reference database files and operate databases in new applications, including how to copy database files in the installation directory to independent storage, how to directly connect to the database files in the installation directory for read-only operations; read and add new groups to operate database files through an address book test project;

Download demo instance

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.