Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
SQL CE & Windows Phone The easy way Paul Scivetti [email protected] @TheIdeaGuy BuzzTheCloud.com January 17, 2012 SQL CE + Win Phone! Table Designer Stored Procs MS Dev Tools T-SQL Entity Framework L2S + “Code First” Seriously? #%*$!! Better Way? •Simple •Useful •Balanced • SQL CE Basics • Tools • Using L2S • Performance Demo SQL CE Basics • Where is the Database? • Create / Modify Database • Create Tables Where is the Database? using System.Data.Linq; public partial class DBDataContext : DataContext { public DBDataContext() : base("Data Source=isostore:/PhoneDemoDB.sdf") { OnCreated(); } } Create Database DBDataContext db = new DBDataContext(); if (!db.DatabaseExists()) { db.CreateDatabase(); var updater = db.CreateDatabaseSchemaUpdater(); updater.DatabaseSchemaVersion = DB_CURRENT_VERSION; updater.Execute(); } Update Schema var updater = db.CreateDatabaseSchemaUpdater(); int dbVersion = updater.DatabaseSchemaVersion ; if (dbVersion < DB_CURRENT_VERSION) { if (dbVersion < 2) { updater.AddTable<Log>(); updater.AddColumn<Food>("Calories"); } updater.DatabaseSchemaVersion = DB_CURRENT_VERSION; updater.Execute(); } Code SQL CE Basics • Where is the Database? • Create / Modify Database • Create Tables #%*$!! • SQL CE Basics • Tools • Using L2S • Performance 1. Design Tables 4. Use L2S in App 2. Run SQLMetal 3. Tweak code SQLMetal • Windows SDK • Command line tool • Generates mapping classes • Almost WinPhone Compatible .\SqlMetal.exe /server:.\sqlexpress /database:phonedemo /code:c:\metal\phonedemo.cs /context:DBDataContext /pluralize /namespace:PhoneApp2 Code • SQL CE Basics • Tools • Using L2S • Performance Query Syntax: DBDataContext db = new DBDataContext(); var qry = from f in db.Foods orderby f.Description select f; foreach (Food item in qry) { // process each food item } Fluent Syntax: DBDataContext db = new DBDataContext(); var qry = db.Events.OrderByDescending(o => o.MeetingDate) .ThenBy(o => o.Topic); foreach (Event item in qry) { // process each event } CRUD: Insert DBDataContext db = new DBDataContext(); Food f = new Food(); f.Description = “Mushroom Pizza”; db.Foods.InsertOnSubmit(f); db.SubmitChanges(); Code • SQL CE Basics • Tools • Using L2S • Performance Async Access using System.Threading.Tasks; private async void LoadFoodAsync() { ObservableCollection<Food> res = new ObservableCollection<Food>(); await TaskEx.Run(() => { DBContext db = new DBContext(); var qry = from s in db.Food select s; res = new ObservableCollection<Food>(qry); }); this.FoodItems = res; } Performance Tuning using System.Data.Linq; public partial class DBDataContext : DataContext { // default buffer size: 384K public DBDataContext() : base("Data Source=isostore: /PhoneDemoDB.sdf;max buffer size=1024") { OnCreated(); } } Resources Windows Phone Development create.msdn.com LINQ to SQL SQL CE & Windows Phone The easy way Paul Scivetti [email protected] @TheIdeaGuy BuzzTheCloud.com January 17, 2012 SQL CE & Windows Phone The easy way Paul Scivetti [email protected] @TheIdeaGuy BuzzTheCloud.com January 17, 2012