在WP7 OS7.1 ,可以將資料存放區在位於隔離儲存區 (Isolated Storage)的本機資料庫中。
使用 LINQ TO SQL 執行所有的操作,包括:定義資料庫結構,查詢資料,儲存改變到位於隔離儲存區 (Isolated Storage)的原資料庫檔案中。
由圖可見,linq to sql模型用於橋接 應用程式和資料庫
Data Context
data context 是一個代理,用於代表資料庫。它包含了許多的表。每一個表都是由對應的實體類組成。
相似和不同(與電腦相比)
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.
添加已有的資料庫檔案步驟:
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.
Extract the local database from the helper application: Use the Isolated Storage Explorer (ISETool.exe) to copy the database from the helper application to a folder on your computer. For more information about the Isolated Storage Explorer, see How to: Use the Isolated Storage Explorer Tool.
Create the primary application: Create the application that will consume the reference data.
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 file as Content.
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, use the appdata: 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 with the Application.GetResourceStream method to create a stream from the installation folder, and the IsolatedStorageFileStream.Write method 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;
使用data context操作資料庫:
1,首先建立 Data context 和實體類:
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> ToDoItems;}// 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) { NotifyPropertyChanging("ToDoItemId"); _toDoItemId = value; NotifyPropertyChanged("ToDoItemId"); } } } . . . . . . . . .
添加引用
using System.Data.Linq;using System.Data.Linq.Mapping;using Microsoft.Phone.Data.Linq;using Microsoft.Phone.Data.Linq.Mapping;
建立資料庫:
// 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(); }}
增刪改查操作具體見:
http://msdn.microsoft.com/en-us/library/hh202860(v=VS.92).aspx
源碼:http://msdn.microsoft.com/en-us/library/ff431744.aspx