详解Windows Phone Mango的本地数据库
创始人
2024-07-31 15:10:39
0

终于在Windows Phone Mango中加入了数据库,使保存、查找、插入数据不再痛苦。在Windows Phone 7中如果要做到这些,要么使用XML自已实现,要么使用第三方数据库,但是第三方数据库要么性能不好,要么占用空间太大,要么收费,现在总算有原生的本地数据库了。

架构

架构

这个本地数据库,不能直接支持Transact-SQL,需要通过LINQ to SQL 对象模型作为Proxy来操作数据库,为此引入了一个新的类System.Data.Linq.DataContext。这与windows mobile上的SQL CE有了很大的差别。

 LINQ to SQL

在Windows Phone中,LINQ to SQL既不能直接支持执行Data Definition Language(DDL)也不支持Data Modeling Language(DML),另外也不能直接访问ADO.NET。只能支持Microsoft SQL Server Compact Edition (SQL CE)的数据类型。并且需要通过DataContext方式来操来数据库。

支持的数据类型

数据类型

描述

bigint

Integer (whole number) data from –2^63 (–9,223,372,036,854,775,808) through 2^63–1 (9,223,372,036,854,775,807). Storage size is 8 bytes.

integer

Integer (whole number) data from –2^31 (–2,147,483,648) through 2^31–1 (2,147,483,647).

Storage size is 4 bytes.

smallint

Integer data from –32,768 to 32,767. Storage size is 2 bytes.

tinyint

Integer data from 0 to 255. Storage size is 1 byte.

bit

Integer data with a value of either 1 or 0.

Storage size is 1 bit.

numeric (p, s)

Synonyms:

decimal(p,s) and dec (p,s)

Fixed-precision and scale-numeric data from –10^38+1 through 10^38–1. The variable specifies precision and can vary between 1 and 38. The s variable specifies scale and can vary between 0 and p.

Storage size is 19 bytes.

money

Monetary data values from (–2^63/10000) (–922,337,203,685,477.5808) through 2^63–1 (922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit. Storage size is 8 bytes.

float

Floating point number data from –1.79E +308 through 1.79E+308

Storage size is 8 bytes.

real

Floating precision number data from –3.40E+38 through 3.40E+38.

Storage size is 4 bytes.

datetime

Date and time data from January 1, 1753, to December 31, 9999, with an accuracy of one three-hundredth second, or 3.33 milliseconds. Values are rounded to increments of .000, .003, or .007 milliseconds.

Stored as two 4-byte integers. The first 4 bytes store the number of days before or after the base date, January 1, 1900. The base date is the system's reference date. Values for datetime earlier than January 1, 1753, are not permitted. The other 4 bytes store the time of day represented as the number of milliseconds after midnight. Seconds have a valid range of 0–59.

Format Example

yyyy/mm/dd hh:mm:ss 1947/08/15 03:33:20

mm/dd/yyyy hh:mm:ss 04/15/1947 03:33:20

dd mmm yyyy hh:mm:ss 15 Jan 1947 03:33:20

dd mmmm yyyy h:mm:ss 15 January 1947 03:33:20

national character(n)

Synonym:nchar(n)

Fixed-length Unicode data with a maximum length of 4000 characters. Default length = 1. Storage size, in bytes, is two times the number of characters entered.

national character varying(n)

Synonym:nvarchar(n)

Variable-length Unicode data with a length of 1 to 4000 characters. Default length = 1. Storage size, in bytes, is two times the number of characters entered.

ntext¹

Variable-length Unicode data with a maximum length of (2^30–2)/2 (536,870,911) characters. Storage size, in bytes, is two times the number of characters entered.

Note

ntext is no longer supported in string functions.

nchar

Fixed-length Unicode character data of n characters. n must be a value from 1 through 4,000. The storage size is two times n bytes.

binary(n)

Fixed-length binary data with a maximum length of 8000 bytes. Default length = 1.

Storage size is fixed, which is the length in bytes declared in the type.

varbinary(n)

Variable-length binary data with a maximum length of 8000 bytes. Default length = 1.

Storage size varies. It is the length of the value in bytes.

image¹

Variable-length binary data with a maximum length of 2^30–1 (1,073,741,823) bytes.

Storage is the length of the value in bytes.

uniqueidentifier

A globally unique identifier (GUID). Storage size is 16 bytes.

IDENTITY [(s, i)]

This is a property of a data column, not a distinct data type.

Only data columns of the integer data types can be used for identity columns. A table can have only one identity column. A seed and increment can be specified and the column cannot be updated.

s (seed) = starting value

i(increment) = increment value

ROWGUIDCOL

This is a property of a data column, not a distinct data type. It is a column in a table that is defined by using the uniqueidentifier data type. A table can have only one ROWGUIDCOL column.

Timestamp/rowversion

This is an automatically generated unique binary number.

Storage size is 8 bytes.

 

在SQL Server Compact 4.0中,当Ntext 和 image 数据超过256 bytes 时将会保存到一个新的数据页。这会影响到数据库的密度,因为SQL Server Compact 4.0 数据库是按页方式面不是按字节方式来压缩的。

创建工程

新建一个Windows Phone工程,最好是MVVM工程,也就是选择新建工程中的Windows Phone Databound Application 模板直接生成或者手工创建MVVM工程。创建好后,将System.Data.Linq命名空间引入到工程。这个命名空间所在位置\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71中。

创建数据库

新建一个类继承System.Data.Linq.DataContext类,这样就可以用这个类来控制数据库了。

  1. public class MyDataContext : DataContext 
  2.     { 
  3.         public const string ConnectionStr = "Data Source=isostore:/MyDB.sdf"; 
  4.         public Table Rows; 
  5.         public MyDataContext() 
  6.             : base(ConnectionStr) 
  7.         { 
  8.            
  9.         } 
  10.     } 

在这个类中同时创建数据库表类。

  1. [Table] 
  2.     public class MyTable : INotifyPropertyChanged, INotifyPropertyChanging 
  3.     { 
  4.         private int _index; 
  5.         [Column(IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true, DbType = "INT NOT NULL Identity", AutoSync=AutoSync.OnInsert)] 
  6.         public int Index 
  7.         { 
  8.             get 
  9.             { 
  10.                 return _index; 
  11.             } 
  12.             set 
  13.             { 
  14.                 if (_index != value) 
  15.                 { 
  16.                     NotifyPropertyChanging("Index"); 
  17.                     _index = value; 
  18.                     NotifyPropertyChanged("Index"); 
  19.                 } 
  20.   
  21.             } 
  22.         } 
  23.   
  24.         private string _name; 
  25.         [Column] 
  26.         public string Name 
  27.         { 
  28.             get 
  29.             { 
  30.                 return _name; 
  31.             } 
  32.             set 
  33.             { 
  34.                 NotifyPropertyChanging("Name"); 
  35.                 _name = value; 
  36.                 NotifyPropertyChanged("Name"); 
  37.             } 
  38.         } 
  39.   
  40.         private String _gen; 
  41.         [Column] 
  42.         public String Gen 
  43.         { 
  44.             get 
  45.             { 
  46.                 return _gen; 
  47.             } 
  48.             set 
  49.             { 
  50.                 NotifyPropertyChanging("Gen"); 
  51.                 _gen = value; 
  52.                 NotifyPropertyChanged("Gen"); 
  53.             } 
  54.         } 
  55.   
  56.         private int _age; 
  57.         [Column] 
  58.         public int Age 
  59.         { 
  60.             get 
  61.             { 
  62.                 return _age; 
  63.             } 
  64.             set 
  65.             { 
  66.                 NotifyPropertyChanging("Age"); 
  67.                 _age = value; 
  68.                 NotifyPropertyChanged("Age"); 
  69.             } 
  70.         } 
  71.   
  72.         #region INotifyPropertyChanged Members 
  73.   
  74.         public event PropertyChangedEventHandler PropertyChanged; 
  75.   
  76.         private void NotifyPropertyChanged(string propertyName) 
  77.         { 
  78.             if (PropertyChanged != null) 
  79.             { 
  80.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
  81.             } 
  82.         } 
  83.   
  84.         #endregion 
  85.   
  86.         #region INotifyPropertyChanging Members 
  87.   
  88.         public event PropertyChangingEventHandler PropertyChanging; 
  89.   
  90.         private void NotifyPropertyChanging(string propertyName) 
  91.         { 
  92.             if (PropertyChanging != null) 
  93.             { 
  94.                 PropertyChanging(this, new PropertyChangingEventArgs(propertyName)); 
  95.             } 
  96.         } 
  97.   
  98.         #endregion 
  99.     } 

操作数据库

新建一个ViewModel类,在这个类中实现数据库的操作。在这个类中实现了对数据的选择、保存、更新和删除。在对数据库的操作因为不能直接使用Transact_SQL,所以操作都是通过LINQ来完成的。

  1. MyDataContext _DB; 
  2. public void SelectData() 
  3.         { 
  4.             if (IsAddedToDB) 
  5.             { 
  6.                 if (_DB.DatabaseExists()) 
  7.                 { 
  8.                     IEnumerator enumerator = _DB.Rows.GetEnumerator(); 
  9.                     while (enumerator.MoveNext()) 
  10.                     { 
  11.                         this.Items.Add(enumerator.Current); 
  12.                     } 
  13.   
  14.                     IsAddedToDB = false; 
  15.   
  16.                 } 
  17.             } 
  18.            
  19.         } 
  20.   
  21.         public bool SaveData(MyTable table) 
  22.         { 
  23.             try 
  24.             { 
  25.                 this.Items.Add(table); 
  26.                 _DB.Rows.InsertOnSubmit(table); 
  27.                 _DB.SubmitChanges(); 
  28.             } 
  29.             catch (Exception e) 
  30.             { 
  31.                 return false; 
  32.             } 
  33.   
  34.             return true; 
  35.         } 
  36.   
  37.         public bool DeleteData(MyTable table) 
  38.         { 
  39.             try 
  40.             { 
  41.                 if (_DB.DatabaseExists()) 
  42.                 { 
  43.                     _DB.Rows.DeleteOnSubmit(table); 
  44.                     _DB.SubmitChanges(); 
  45.                     this.Items.Remove(table); 
  46.                 } 
  47.             } 
  48.             catch (Exception e) 
  49.             { 
  50.                 return false; 
  51.             } 
  52.   
  53.             return true; 
  54.         } 
  55.   
  56.         public bool UpdateData(MyTable source, MyTable dest) 
  57.         { 
  58.             try 
  59.             { 
  60.                 var tables = from item in this.Items where (int)item.Index == source.Index select item; 
  61.   
  62.                 foreach (MyTable mt in tables) 
  63.                 { 
  64.                     mt.Name = dest.Name; 
  65.                     mt.Age = dest.Age; 
  66.                     mt.Gen = dest.Gen; 
  67.                     break; 
  68.                 } 
  69.   
  70.                 MyTable table = _DB.Rows.GetOriginalEntityState(source); 
  71.                 table.Name = dest.Name; 
  72.                 table.Age = dest.Age; 
  73.                 table.Gen = dest.Gen; 
  74.                 _DB.SubmitChanges(); 
  75.             } 
  76.             catch (Exception e) 
  77.             { 
  78.                 return false; 
  79.             } 
  80.   
  81.             return true; 
  82.         } 

下是运行效果:

运行效果

运行效果

运行效果

运行效果

原文地址:http://www.cnblogs.com/randylee/archive/2011/06/13/2080167.html

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...