400 likes | 538 Views
The easy way. SQL CE & Windows Phone. Paul Scivetti paul@synergensoftware.com @ 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?. #%*$!!. SQL CE . Better
E N D
The easy way SQL CE & Windows Phone Paul Scivetti paul@synergensoftware.com @TheIdeaGuy BuzzTheCloud.com • January 17, 2012
SQL CE + Win Phone!
Table Designer Stored Procs MS Dev Tools T-SQL Entity Framework
#%*$!! SQL CE
Better Way?
Simple • Useful • Balanced
SQL CE Basics • Tools • Using L2S • Performance
SQL CE Basics • Where is the Database? • Create / Modify Database • Create Tables
Where is the Database? usingSystem.Data.Linq; public partialclass 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(); intdbVersion = updater.DatabaseSchemaVersion ; if (dbVersion < DB_CURRENT_VERSION) { if (dbVersion < 2) { updater.AddTable<Log>(); updater.AddColumn<Food>("Calories"); } updater.DatabaseSchemaVersion = DB_CURRENT_VERSION; updater.Execute(); }
SQL CE Basics • Where is the Database? • Create / Modify Database • Create Tables
#%*$!! SQL CE
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 • AlmostWinPhone Compatible
.\SqlMetal.exe /server:.\sqlexpress/database:phonedemo/code:c:\metal\phonedemo.cs/context:DBDataContext/pluralize/namespace:PhoneApp2
SQL CE Basics • Tools • Using L2S • Performance
Query Syntax: DBDataContext db = new DBDataContext(); varqry = from f in db.Foods orderbyf.Description select f; foreach (Food item in qry) { // process each food item }
Fluent Syntax: DBDataContext db = new DBDataContext(); varqry = 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();
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(); varqry = from s in db.Food select s; res = new ObservableCollection<Food>(qry); }); this.FoodItems = res; }
Performance Tuning usingSystem.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
The easy way SQL CE & Windows Phone Paul Scivetti paul@synergensoftware.com @TheIdeaGuy BuzzTheCloud.com • January 17, 2012
The easy way SQL CE & Windows Phone Paul Scivetti paul@synergensoftware.com @TheIdeaGuy BuzzTheCloud.com • January 17, 2012