Top Banner
LINQ to SQL on Windows Phone Chris Koenig [email protected] | @chriskoenig | http://chriskoenig.net Northwest Arkansas .NET User Group – January 2012
19
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Databases for Windows Phone

LINQ to SQL on Windows Phone Chris [email protected] | @chriskoenig | http://chriskoenig.netNorthwest Arkansas .NET User Group – January 2012

Page 2: Databases for Windows Phone

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

Page 3: Databases for Windows Phone

http://aka.ms/wpsdk

Page 4: Databases for Windows Phone

http://aka.ms/databaseformango

http://aka.ms/WP7Resources

Page 5: Databases for Windows Phone

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

Page 6: Databases for Windows Phone

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

Page 7: Databases for Windows Phone

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

Page 8: Databases for Windows Phone

Demo

Creating a databaseCRUD operations

Page 9: Databases for Windows Phone

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

Page 10: Databases for Windows Phone

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

Page 11: Databases for Windows Phone

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

Page 12: Databases for Windows Phone

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();}

Page 13: Databases for Windows Phone

Demo

Updating your database

Page 14: Databases for Windows Phone

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); }}

Page 15: Databases for Windows Phone

Demo

Deploying a database with your app

Page 16: Databases for Windows Phone

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

Page 17: Databases for Windows Phone

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!

Page 18: Databases for Windows Phone

Chris [email protected]://chriskoenig.net@chriskoenig214-385-5616

Q&A

Page 19: Databases for Windows Phone

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.