Local Database Overview for Windows Phone

來源:互聯網
上載者:User

在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.

添加已有的資料庫檔案步驟:

  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 (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.

  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 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.