LINQ to SQL on Windows Phone Chris Koenig [email protected] | @chriskoenig | http://chriskoenig.net Northwest Arkansas .NET User Group – January 2012
LINQ to SQL on Windows Phone Chris [email protected] | @chriskoenig | http://chriskoenig.netNorthwest Arkansas .NET User Group – January 2012
Windows Phone2
Agenda
Overview of Windows Phone database support
Creating and interrogating a database
Updating your database with your applications
Deploying a reference database with your app
http://aka.ms/wpsdk
http://aka.ms/databaseformango
http://aka.ms/WP7Resources
Windows Phone5
SQL Server on Windows Phone Basedon SQL Server Compact Edition 3.5 Runtime is distributed with the operating
system Interaction managed through LINQ to SQL’s
code-first model Create classes that represent tables Annotate classes and fields/properties
with[Table] and [Column] attributes
Create a custom DataContext object to manage interactions
Automatic conversion of datatypes Properties are available
Encrypt the database using a password on the connection string
Windows Phone6
Local Data Storage: OverviewApps store private data in Isolated Storage
Settings and properties in the app dictionaryUnstructured data in Isolated Storage files Structured data in database files
ApplicationSettings file
AppCreates/managesfiles and settings
Applicationfiles
App Data Folder
Package Manager
App Root Folder
WP7 Isolated Storage APIs
Install
DBDatabase file
Databasefile (r/o)
Creates root folder
sandboxed to App
DB
DB
DataContextName Little
Penguin
Varietal Pinot Noir
AtHome True
Inserts/Updates/Deletes Actions made on the objects and
collections Create new objects Add objects to collections
Managed by the DataContext Changes made against
the DataContext first Changes persisted
by calling SubmitChanges() SubmitChanges
LINQ to SQL determines change set and submits to DB
Name Little Penguin
Varietal Pinot Noir
AtHome False
Your app code
Name Yellow Tail
Varietal Pinot Noir
AtHome True
Demo
Creating a databaseCRUD operations
Windows Phone9
Updating an existing database
Start with a good schema Buddy-project? Get a copy from the Isolated Storage Tool
Make updates to your Model classes Keep track of ALL the changes to your model so you know how
to “add” them later Update the schema at Runtime
Use the DatabaseSchemaUpdater to push changes into the database
Check new version against current version to determine upgrade path
Windows Phone10
Isolated Storage Toolc:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorerTool <ts|rs|dir[:device-folder]>
ts = Take Snapshot rs = Restore Snapshot dir = show directory contents (optional path can be supplied)
<xd|de> xd = run on the emulator de = run on the device
<Product GUID> GUID taken from the WMAppManifest.xml
[<desktop-path>] Optional – desktop path for download and upload
Windows Phone11
IsoStoreSpy Graphical tool for browsing the
Isolated Storage areas on the Phone Device and Emulator
Open Source project on CodePlex
http://isostorespy.codeplex.com/
Multiple file formats supported Text Files Images Multimedia Ringtones SQL CE Databases
Windows Phone12
DatabaseSchemaUpdaterMake schema changes to existing databases
AddTable<T>() AddColumn<T>() AddIndex<T>() AddAssociation<T>() DatabaseSchemaVersio
nMake sure that your new Model classes are already created, and then supply them to the DatabaseSchemaUpdater
var updater = db.CreateDatabaseSchemaUpdater(); if (updater.DatabaseSchemaVersion < 2){ updater.AddTable<History>(); updater.AddColumn<Widget>("CreationDate"); updater.DatabaseSchemaVersion = 2; updater.Execute();}
Demo
Updating your database
Windows Phone14
Deploying a database with your application Use a Buddy Project to create the initial SDF Add the database to your phone project
Set properties as Content, with the “Copy Always” option Use as a read-only database
Copy to Isolated Storage to use as a read-write database
var db = new MyDataContext("Data Source='appdata:/ReferenceDB.sdf';File Mode=read only;");
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); Uri uri = new Uri("ReferenceDB.sdf", UriKind.Relative); using (Stream input = Application.GetResourceStream(uri).Stream) using (IsolatedStorageFileStream output = iso.CreateFile(“ApplicationDB.sdf")) { byte[] readBuffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0) { output.Write(readBuffer, 0, bytesRead); }}
Demo
Deploying a database with your app
Windows Phone16
Agenda
Overview of Windows Phone database support
Creating and interrogating a database
Updating your database with your applications
Deploying a reference database with your app
Windows Phone17
Call to Action
Download the tools from http://aka.ms/wp7sdk Download the Starter Kits from
http://code.msdn.microsoft.com Register for AppHub at http://create.msdn.com Have fun getting rich building apps for Windows
Phone!
Chris [email protected]://chriskoenig.net@chriskoenig214-385-5616
Q&A
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.
MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
© 2011 Microsoft Corporation.
All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.