[07.20]Windows Phone Mango Local DataBase 本地数据库实用初步

芒果推出了本地数据库之后,要求运用 Linq to SQL 进行数据控制。

数据库和界面逻辑对应起来的实现形式是这样的:

1,用数据库逻辑和标记直接建立 Model,一般继承自INotifyPropertyChanged, INotifyPropertyChanging;

2,和 DataContext 对应起来,继承DataContet 建立子类,在类中进行数据库的链接,定义Table集合。这个类是数据库和程序主体的桥梁;

3,在 ViewModel 中新建自己建立的DataContext 子类的对象,并且通过Linq 将其和ObservableCollection 进行对接;

4,在 View 中绑定。

Tips:

1,在CheckBox,TextBox这类控件进行了双向绑定后,手动更改界面值不仅仅会造成起ViewModel 中的ObservableCollection 发生改变,甚至可以直接影响到 DataContext 链接的数据库;

2,数据库更改后注意各种提交,InsertOnSubmit(),DeleteOnSubmit(),保存数据库用SubmitChanges();

3,可以使用DatabaseSchemaUpdater 可用来更新数据库的结构,自身结构等级,添加列等,调用 DatabaseSchemaUpdater.Execute() 执行更新;

4,数据库的Connection Strings 链接参数,在DataContext 构造函数中选择,形式为:DataContext(string connectionStrings);

Parameter

Description

data source ordatasource

The file path and name of the local database file. When this is the only connection string property specified, only the value of the property is required to instantiate a data context object. For more information about a data context, seeLocal Database Overview for Windows Phone.

Use the following prefixes to explicitly specify the root location of the path:

  • isostore: path is applied to isolated storage

  • appdata: path is applied to the installation folder

When a prefix is not specified, the file path is applied to isolated storage.

Password or Pwd ordatabase password orssce:database password

The database password, which can be up to 40 characters in length. If not specified, the default value is no password. This property is required if you enable encryption on the database. If you specify a password, encryption is automatically enabled on the database. If you specify a blank password, the database will not be encrypted.

NoteNote:
You cannot encrypt a database after it has been created.

max buffer size orssce:max buffer size

The largest amount of memory, in kilobytes, that a local database can use before it starts flushing changes to disk. If not specified, the default value is 384. The maximum value is 5120.

max database size orssce:max database size

The maximum size of a local database, in megabytes. If not specified, the default value is 32. The maximum value is512.

Mode or file mode orssce:mode

The mode to use when opening the database file. The following values are valid:

  • Read Write: Allows multiple processes to open and modify the database. This is the default setting if the mode property is not specified.

  • Read Only: Allows you to open a read-only copy of the database.

  • Exclusive: Does not allow other processes from opening or modifying the database.

  • Shared Read: Allows other processes to read, but not modify, the database while you have it open.

Culture Identifier

The culture code to use with the database. For example, en-US for United States English. For the full list of supported culture codes in Windows Phone OS 7.1, see Culture and Language Support for Windows Phone.

NoteNote:
This property is ignored if used when connecting to an existing database.

Case Sensitive orCaseSensitive

A Boolean value that determines whether or not the database collation is case-sensitive. Must be set to true to enable case-sensitive collation or false for case-insensitive collation. If not specified, the default value is false.

NoteNote:
This property is ignored if used when connecting to an existing database.
Important note Important Note:

Some Microsoft SQL Compact connection string parameters may work in this release of the Windows Phone Application Platform. We do not recommend using any connection string parameters that are not listed in this topic.

5,可以用 [Association] 标签关联两个数据库,下面摘自MSDN 关于数据库属性标签的说明:

Attribute

Example

Description

TableAttribute

[Table]

Designates a class as an entity class that is associated with a database table.

ColumnAttribute

[Column(IsPrimaryKey = true)]

Associates a class with a column in a database table. IsPrimaryKeyspecifies the primary key, for which an index is created by default.

IndexAttribute

[Index(Columns="Column1,Column2", IsUnique=true, Name="MultiColumnIndex")]

Written at the table level, designates additional indexes on the table. Each index can cover one or more columns.

AssociationAttribute

[Association(Storage="ThisEntityRefName", ThisKey="ThisEntityID", OtherKey="TargetEntityID")]

Designates a property to represent an association, such as a foreign key to primary key association.

posted @ 2011-07-20 11:23  akita  阅读(552)  评论(0编辑  收藏  举报