Local Database Overview for Windows Phone

在WP7 OS7.1 ,可以将数据存储在位于独立存储的本地数据库中。

使用 LINQ TO SQL 执行所有的操作,包括:定义数据库结构,查询数据,保存改变到位于独立存储的原数据库文件中。

Windows Phone DataContext and Local Database 

由图可见,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://blogs.msdn.com/b/matt/archive/2008/05/22/into-to-linq-to-sql-optimistic-concurrency.aspx

增删改查操作具体见:

http://msdn.microsoft.com/en-us/library/hh202860(v=VS.92).aspx

源码:http://msdn.microsoft.com/en-us/library/ff431744.aspx

原文地址:https://www.cnblogs.com/jeekun/p/2108967.html