博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
08,Windows Phone 本地存储
阅读量:2503 次
发布时间:2019-05-11

本文共 2419 字,大约阅读时间需要 8 分钟。

内容预告:

  • 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
Wines;public Table
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
("BottleType");dsu.DatabaseSchemaVersion = 2;dsu.Execute();}

性能和最佳实践:

  • 保持修改的集合很小,换句话说,尽早提交修改,以避免程序终止时数据丢失。
  • 用后台线程。
  • 优化只读查询。
  • 提前填充大量数据。
  • 用对的工具,大量复杂的数据用数据库,小数据用独立存储。

 

 

 

 

 

 

 

 

 

 

转载地址:http://cllgb.baihongyu.com/

你可能感兴趣的文章
IOS 简单的动画自定义方法(旋转、移动、闪烁等)
查看>>
js/jquery 实时监听输入框值变化的完美方案:oninput & onpropertychange
查看>>
数论入门
查看>>
Driving the Activity Lifecycle
查看>>
axios
查看>>
PostgreSQL导出一张表到MySQL
查看>>
MVC 前台向后台传输数据
查看>>
《少年先疯队》第四次作业:项目需求调研与分析
查看>>
IPv6 Scapy Samples
查看>>
Asp.Net Ajax的两种基本开发模式
查看>>
哈希——并查集结构——岛问题
查看>>
正则表达式
查看>>
图像处理笔记(十二)
查看>>
条件数(condition number)
查看>>
你还在问android横竖屏切换的生命周期?
查看>>
判断整数是否能被n整除
查看>>
Chapter 3 Phenomenon——9
查看>>
win64 Python下安装PIL出错解决2.7版本 (3.6版本可以使用)
查看>>
获取各种类型的节点
查看>>
表达式求值-201308081712.txt
查看>>