08,Windows Phone 本機存放區

來源:互聯網
上載者:User

內容預告:

  • Windows Phone 的資料庫支援
  • LINQ to SQL
  • 效能和最佳實務

LINQ to Everything:

支援複雜的結構:

支援外鍵:

WebService緩衝:

本機存放區:

架構:

對象:

定義表:

  // Define the tables in the database  
[Table]
public class Wine : INotifyPropertyChanged, INotifyPropertyChanging
{
private string wineID;
private string name;
[Column(IsPrimaryKey=true)]
public string WineID
{
get { return wineID; }
set {        
InvokePropertyChanging(new PropertyChangingEventArgs("WineID"));             
wineID = value;             
InvokePropertyChanged(new PropertyChangedEventArgs("WineID"));        
}  
}
[Column]
public string Name { ... }
...}

定義資料內容:

// Define the data context.public partial class WineDataContext : DataContext {public Table<Wine> Wines;public Table<Vineyard> Vineyards;public WineDataContext(string connection) : base(connection) { }}...// Create the database from data context, using a connection stringDataContext db = new WineDataContext("isostore:/wineDB.sdf");if (!db.DatabaseExists())     db.CreateDatabase();

用SQLMetal代碼產生工具:

c:\>Sqlmetal /code:northwindEntities.cs                   
/context:NorthwindDataContext
/pluralize northwind.sdf

查詢:

 // Create the database form data context, using a connection string 
DataContext db = new WineDataContext("isostore:/wineDB.sdf"); // Find all wines currently at home, ordered by date acquired
var q = from w in db.Wines
where w.Varietal.Name == “Shiraz” && w.IsAtHome == true
orderby w.DateAcquired select w;

插入,更新,刪除:別忘了submitChanges

插入

Wine newWine = new Wine{WineID = “1768",Name = “Windows Phone Syrah",Description = “Bold and spicy"};db.Wines.InsertOnSubmit(newWine);db.SubmitChanges();

更新:

Wine wine = (from w in db.Wines where w.WineID == “1768" select w).First();wine.Description = “Hints of plum and melon";db.SubmitChanges();

刪除:

var vineyardsToDelete = from Vineyards v in db.Vineyardswhere v.Country == “Australia”select v;db.Vineyards.DeleteAllOnSubmit(vineyardsToDelete);            db.SubmitChanges();

更新資料庫結構:

WineDataContext wineDC = new WineDataContext(App.WineDBConnectionString);DatabaseSchemaUpdater dsu = wineDC.CreateDatabaseSchemaUpdater();if (dsu.DatabaseSchemaVersion == 1){dsu.AddColumn<Wine>("BottleType");dsu.DatabaseSchemaVersion = 2;dsu.Execute();} 

效能和最佳實務:

  • 保持修改的集合很小,換句話說,儘早提交修改,以避免程式終止時資料丟失。
  • 用後台線程。
  • 最佳化唯讀查詢。
  • 提前填充大量資料。
  • 用對的工具,大量複雜的資料用資料庫,小資料用隔離儲存區 (Isolated Storage)。

 

 

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.