如果在winsows phone中使用資料庫,可以使用SQLMetal,它不但能把SQL Server中的資料生成實體應用在WP中,還可以很友善的使用Linq來對資料庫各實體進行操作。可以說是手機開發與桌面及web開發實作無縫的開發體驗。
<b>1、 </b><b>建立</b><b>SQL</b><b>資料庫</b>
<a href="http://blog.51cto.com/attachment/201201/175307324.png" target="_blank"></a>
<b>2、 </b><b>把</b><b>SQL</b><b>庫導成實體</b><b>CSharp</b><b>源代碼</b>
安裝完WP開發環境後,會在安裝路徑下安裝一個工具sqletal,這個工具可以幫助我們把SQL Server資料庫轉換成WP中應用的CSharp源代碼。
在cmd指令下鍵入:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin>sqlmetal /server:. /database:accou
nt_db /namespace:test /code:f:/test.cs /language:csharp
server:指sql server伺服器名
database:資料庫名
namespace:生成的命名空間
code:生成源代碼的路徑和檔案名
language:生成的語言
生成的源碼如下:
#pragma warning disable 1591
namespace test
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[System.Data.Linq.Mapping.DatabaseAttribute(Name="account_db")]
public partial class Account_db : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertAccounts(Accounts instance);
partial void UpdateAccounts(Accounts instance);
partial void DeleteAccounts(Accounts instance);
partial void InsertItems(Items instance);
partial void UpdateItems(Items instance);
partial void DeleteItems(Items instance);
#endregion
public Account_db(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public Account_db(System.Data.IDbConnection connection) :
public Account_db(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
public Account_db(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
public System.Data.Linq.Table<Accounts> Accounts
get
{
return this.GetTable<Accounts>();
}
public System.Data.Linq.Table<Items> Items
return this.GetTable<Items>();
}
[Table(Name="dbo.Accounts")]
public partial class Accounts : INotifyPropertyChanging, INotifyPropertyChanged
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _ID;
private System.Nullable<decimal> _Amount;
private System.Nullable<System.DateTime> _Dtime;
private System.Nullable<int> _ItemID;
private EntityRef<Items> _Items;
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnIDChanging(int value);
partial void OnIDChanged();
partial void OnAmountChanging(System.Nullable<decimal> value);
partial void OnAmountChanged();
partial void OnDtimeChanging(System.Nullable<System.DateTime> value);
partial void OnDtimeChanged();
partial void OnItemIDChanging(System.Nullable<int> value);
partial void OnItemIDChanged();
public Accounts()
this._Items = default(EntityRef<Items>);
[Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ID
return this._ID;
set
if ((this._ID != value))
{
this.OnIDChanging(value);
this.SendPropertyChanging();
this._ID = value;
this.SendPropertyChanged("ID");
this.OnIDChanged();
}
[Column(Storage="_Amount", DbType="Money")]
public System.Nullable<decimal> Amount
return this._Amount;
if ((this._Amount != value))
this.OnAmountChanging(value);
this._Amount = value;
this.SendPropertyChanged("Amount");
this.OnAmountChanged();
[Column(Storage="_Dtime", DbType="DateTime")]
public System.Nullable<System.DateTime> Dtime
return this._Dtime;
if ((this._Dtime != value))
this.OnDtimeChanging(value);
this._Dtime = value;
this.SendPropertyChanged("Dtime");
this.OnDtimeChanged();
[Column(Storage="_ItemID", DbType="Int")]
public System.Nullable<int> ItemID
return this._ItemID;
if ((this._ItemID != value))
if (this._Items.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnItemIDChanging(value);
this._ItemID = value;
this.SendPropertyChanged("ItemID");
this.OnItemIDChanged();
[Association(Name="FK_Accounts_Items", Storage="_Items", ThisKey="ItemID", OtherKey="ID", IsForeignKey=true)]
public Items Items
return this._Items.Entity;
Items previousValue = this._Items.Entity;
if (((previousValue != value)
|| (this._Items.HasLoadedOrAssignedValue == false)))
if ((previousValue != null))
this._Items.Entity = null;
previousValue.Accounts.Remove(this);
this._Items.Entity = value;
if ((value != null))
value.Accounts.Add(this);
this._ItemID = value.ID;
else
this._ItemID = default(Nullable<int>);
this.SendPropertyChanged("Items");
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
if ((this.PropertyChanging != null))
this.PropertyChanging(this, emptyChangingEventArgs);
protected virtual void SendPropertyChanged(String propertyName)
if ((this.PropertyChanged != null))
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
[Table(Name="dbo.Items")]
public partial class Items : INotifyPropertyChanging, INotifyPropertyChanged
private string _ItemName;
private EntitySet<Accounts> _Accounts;
partial void OnItemNameChanging(string value);
partial void OnItemNameChanged();
public Items()
this._Accounts = new EntitySet<Accounts>(new Action<Accounts>(this.attach_Accounts), new Action<Accounts>(this.detach_Accounts));
[Column(Storage="_ItemName", DbType="NVarChar(50)")]
public string ItemName
return this._ItemName;
if ((this._ItemName != value))
this.OnItemNameChanging(value);
this._ItemName = value;
this.SendPropertyChanged("ItemName");
this.OnItemNameChanged();
[Association(Name="FK_Accounts_Items", Storage="_Accounts", ThisKey="ID", OtherKey="ItemID", DeleteRule="NO ACTION")]
public EntitySet<Accounts> Accounts
return this._Accounts;
this._Accounts.Assign(value);
private void attach_Accounts(Accounts entity)
this.SendPropertyChanging();
entity.Items = this;
}
private void detach_Accounts(Accounts entity)
entity.Items = null;
}
#pragma warning restore 1591
<b>3、 </b><b>把</b><b>SQL</b><b>實體代碼添加到項目中</b>
A. 添加項目引用System.Data.Linq
B. 注釋掉用System.Data.IDbConnection的構造函數,因為在WP中沒有IDbConnection接口
C. 保持資料庫實體對象中,表集合的命名是複數形式(注意是Account_db類中),即Accounts和Items
D. 實體類Table特性類的Name屬性不要帶dbo,如下:
[Table(Name="dbo.Accounts")]改成[Table(Name="Accounts")]
E. 實體類中的屬性如果是string類型,最好用Column特性的DbType命名參數用NVarchar,如下:[Column(Storage="_ItemName", DbType="NVarChar(50)")]
<b>4、 </b><b>在</b><b>Windows Phone</b><b>下生成資料庫</b>
可以在App類的Application_Launching事件方法中寫入如下代碼來建立WP中的資料庫對象:
using (Test.Account_db db = new Account_db("Data Source='isostore:/test.sdf';Password=123"))
{
if (db.DatabaseExists() == false)
{
db.CreateDatabase();
}
}
<b>5、 </b><b>操作資料</b>
操作資料的方法與 web和桌面開發是相同的,使用Linq To SQL方法即可。下面是Items實體對象的添加。
Account_db accountdb = new Account_db("Data Source='isostore:/test.sdf';Password='saiko2011'");
Items item = new Items();
item.ItemName = "收入";
accountdb.Items.InsertOnSubmit(item);
accountdb.SubmitChanges();
lb.ItemsSource = accountdb.Items;
本文轉自桂素偉51CTO部落格,原文連結:http://blog.51cto.com/axzxs/762688 ,如需轉載請自行聯系原作者