data access mobile devices

30
Mobile Data with the Compact Framework Shawn Wildermuth Senior Consultant/Architect Magenic Technologies

Upload: venkat987

Post on 10-May-2015

3.285 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Data Access Mobile Devices

Mobile Data with the Compact FrameworkMobile Data with the Compact Framework

Shawn WildermuthSenior Consultant/Architect

Magenic Technologies

Shawn WildermuthSenior Consultant/Architect

Magenic Technologies

Page 2: Data Access Mobile Devices

Who I AmWho I Am

Shawn Wildermuth ([email protected])

C# MVP INETA Speaker Book Author – “Pragmatic ADO.NET”

Editor of http://ONDotnet.com This Presentation can be found at:– http://adoguy.com/presentations

Shawn Wildermuth ([email protected])

C# MVP INETA Speaker Book Author – “Pragmatic ADO.NET”

Editor of http://ONDotnet.com This Presentation can be found at:– http://adoguy.com/presentations

Page 3: Data Access Mobile Devices

AgendaAgenda

How Mobile Data is Different Deciding on a Solution Limitations of the Compact Framework Using SqlClient Using SQL Server CE Using Web Services Data Binding on the Compact

Framework SQL Server CE 2005 (Yukon)

How Mobile Data is Different Deciding on a Solution Limitations of the Compact Framework Using SqlClient Using SQL Server CE Using Web Services Data Binding on the Compact

Framework SQL Server CE 2005 (Yukon)

Page 4: Data Access Mobile Devices

How Mobile Data is DifferentHow Mobile Data is Different

The form factor is small– Not much screen real estate– Not much memory room

Needs to work disconnected– Very few Mobile devices are always

connected– Smaller bandwidth usually when

connected

The form factor is small– Not much screen real estate– Not much memory room

Needs to work disconnected– Very few Mobile devices are always

connected– Smaller bandwidth usually when

connected

Page 5: Data Access Mobile Devices

Deciding on a SolutionDeciding on a Solution

Is a Smart Client the right solution?– How much data manipulation is needed?– Is offline support needed?– Does state need to be shared with

servers If Smart Client, which Data Access?– Options include:

SQL Server Data Access SQL Server CE Data Access Web Services

Is a Smart Client the right solution?– How much data manipulation is needed?– Is offline support needed?– Does state need to be shared with

servers If Smart Client, which Data Access?– Options include:

SQL Server Data Access SQL Server CE Data Access Web Services

Page 6: Data Access Mobile Devices

Limitations of the CFLimitations of the CF

System.Data– DataSets work fine– Typed DataSets don’t– No Design-time Support

System.Xml– No XML Serialization Support– OpenNETCF’s Xml Serialization isn’t

Complete System.Runtime.Serialization– No SOAP or Binary Formatters

System.Data– DataSets work fine– Typed DataSets don’t– No Design-time Support

System.Xml– No XML Serialization Support– OpenNETCF’s Xml Serialization isn’t

Complete System.Runtime.Serialization– No SOAP or Binary Formatters

Page 7: Data Access Mobile Devices

Limitations of the CF (2)Limitations of the CF (2)

SqlClient– Only on CE and Pocket PC (no SmartPhone)– Requires Connectivity

SQL CE– Only on CE and Pocket PC (no SmartPhone)

Web Services– Support on all platforms– Somewhat slower (not compressed like

above)

SqlClient– Only on CE and Pocket PC (no SmartPhone)– Requires Connectivity

SQL CE– Only on CE and Pocket PC (no SmartPhone)

Web Services– Support on all platforms– Somewhat slower (not compressed like

above)

Page 8: Data Access Mobile Devices

Limitations of the CF (3)Limitations of the CF (3)

DataSet is limited– Merge() and GetChanges() are missing– Deal with changes row-by-row

DataSet is limited– Merge() and GetChanges() are missing– Deal with changes row-by-row

Page 9: Data Access Mobile Devices

SQL Client Data AccessSQL Client Data Access

Remarkable similar to SqlClient in Full Fx– Full Set of Adapter Functionality

Supported– Connection Strings are to real SQL Server

Remarkable similar to SqlClient in Full Fx– Full Set of Adapter Functionality

Supported– Connection Strings are to real SQL Server

// Create the connection and commandSqlConnection conn = new SqlConnection("...");SqlCommand cmd = conn.CreateCommand();cmd.CommandText = "SELECT * FROM Customers";

// Create a DataSet to put our results inDataSet ds = new DataSet();

// Create the adapterSqlDataAdapter da = new SqlDataAdapter(cmd);

// Fill itda.Fill(ds, "Customers");

Page 10: Data Access Mobile Devices

SQL Client Data Access (2)SQL Client Data Access (2)

Readers are also supported– Same caveats apply to Mobile Apps– Connected apps are hard on low

bandwidth

Readers are also supported– Same caveats apply to Mobile Apps– Connected apps are hard on low

bandwidth// Create the connection and commandSqlConnection conn = new SqlConnection("...");SqlCommand cmd = conn.CreateCommand();cmd.CommandText = "SELECT * FROM Customers";

// Use a Readertry { conn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { string name = rdr["CompanyName"].ToString(); }}finally { conn.Close();}

Page 11: Data Access Mobile Devices

SQL Server CESQL Server CE

CLIENTCLIENT SERVERSERVER

QP/Cursor Engine/ES QP/Cursor Engine/ES

Storage Engine / Repl Storage Engine / Repl TrackingTracking

SQL CE Edition v2.0SQL CE Edition v2.0

OLEDBOLEDB

OLEDBOLEDB

OLEDB CEOLEDB CE

CLR / .NET CFCLR / .NET CF

SQL Server CE Data ProviderSQL Server CE Data Provider

ADO.NETADO.NET

VS .NET (VB.NET, C#)VS .NET (VB.NET, C#)

..NET CF / ManagedNET CF / Managed StackStack

IISIIS

Server Server Agent: Agent:

ReplicationReplicationandand

Remote Remote Data Data

AccessAccessHTTPHTTP

802.11b, 802.11b, CDPD, CDPD, GSM, GSM,

CDMA, CDMA, TDMA, etc.TDMA, etc.

Occasionally Occasionally ConnectedConnected

Data ProviderData Provider

Client Client Agent: Agent:

ReplicationReplicationandandRDARDA

Page 12: Data Access Mobile Devices

SQL Server CE (2)SQL Server CE (2)

Two Methodologies– Remote Data Access

Local Database Push and Pull Access from SQL Server Optional Change Tracking

– Merge Replication Use Replication Engine to Sync Changes Use Subscription model instead of RDA Model Allows vertical and horizontal partitioning

Two Methodologies– Remote Data Access

Local Database Push and Pull Access from SQL Server Optional Change Tracking

– Merge Replication Use Replication Engine to Sync Changes Use Subscription model instead of RDA Model Allows vertical and horizontal partitioning

Page 13: Data Access Mobile Devices

Setting up SQL Server CE - RDASetting up SQL Server CE - RDA

Integrates with IIS– ISAPI Application to access remote data

E.g. http://<machinename>/sqlce/sscesa20.dll

Compresses results across the wire

– SqlCeRemoteDataAccess Takes an URL to the ISAPI App; and Connection String to database on mobile

device

Integrates with IIS– ISAPI Application to access remote data

E.g. http://<machinename>/sqlce/sscesa20.dll

Compresses results across the wire

– SqlCeRemoteDataAccess Takes an URL to the ISAPI App; and Connection String to database on mobile

deviceSqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess("http://shawnw-lptd/sqlce/sscesa20.dll", "Data Source=northwind.sdf");

Page 14: Data Access Mobile Devices

SQL Server CE - RDASQL Server CE - RDA

Pulling Data to Mobile Device– Creation of the local DB with ad-hoc

queries– Uses an OLEDB Connection String to

connect– Pull to bring data from remote db to

mobile db– Can include tracking information for

updating

Pulling Data to Mobile Device– Creation of the local DB with ad-hoc

queries– Uses an OLEDB Connection String to

connect– Pull to bring data from remote db to

mobile db– Can include tracking information for

updating

string rdaOleDbConnectString = "Provider=sqloledb;Data Source=shawnw-lptd;" + "Initial Catalog=Northwind;User Id=Sample;Password=ADONET";

rda.Pull("Customers", "SELECT * FROM Customers", rdaOleDbConnectString, RdaTrackOption.TrackingOnWithIndexes);rda.Pull("Products", "SELECT * FROM Products", rdaOleDbConnectString, RdaTrackOption.TrackingOnWithIndexes);rda.Pull("Orders", "SELECT * FROM Orders", rdaOleDbConnectString, RdaTrackOption.TrackingOnWithIndexes);rda.Pull("OrderDetails", "SELECT * FROM OrderDetails", rdaOleDbConnectString, RdaTrackOption.TrackingOnWithIndexes);

Page 15: Data Access Mobile Devices

SQL Server CE – RDA (2)SQL Server CE – RDA (2)

Uses local database storage– Use of SqlCe* classes to access data

normally– Supports full suite of classes

Uses local database storage– Use of SqlCe* classes to access data

normally– Supports full suite of classes

// Create the connection and commandSqlCeConnection conn = new SqlCeConnection("...");SqlCeCommand cmd = conn.CreateCommand();cmd.CommandText = "SELECT * FROM Customers";

// Create a DataSet to put our results inDataSet ds = new DataSet();

// Create the adapterSqlCeDataAdapter da = new SqlCeDataAdapter(cmd);

// Fill itda.Fill(ds, "Customers");

Page 16: Data Access Mobile Devices

SQL Server CE – RDA (3)SQL Server CE – RDA (3)

Pushing data back– Pushes changed rows back to remote DB– Only if tracking is enabled– Batching does all rows in single Tx

Pushing data back– Pushes changed rows back to remote DB– Only if tracking is enabled– Batching does all rows in single Tx

rda.Push("Customers", rdaOleDbConnectString, RdaBatchOption.BatchingOn);rda.Push("Products", rdaOleDbConnectString, RdaBatchOption.BatchingOn);rda.Push("Orders", rdaOleDbConnectString, RdaBatchOption.BatchingOn);rda.Push("OrderDetails", rdaOleDbConnectString, RdaBatchOption.BatchingOn);

Page 17: Data Access Mobile Devices

SQL Server CE – Merge ReplicationSQL Server CE – Merge Replication

Retrieves Data with Replication– Fills the local DB from replication

subscription– Like RDA, uses ISAPI Application– And local database connection for local

cache

Retrieves Data with Replication– Fills the local DB from replication

subscription– Like RDA, uses ISAPI Application– And local database connection for local

cacheSqlCeReplication repl = new SqlCeReplication();repl.InternetUrl = "http://shawnw-lptd/sqlce/sscesa20.dll";repl.Publisher = "SHAWNW-LPTD";repl.PublisherDatabase = "Northwind";repl.PublisherLogin = "sample";repl.PublisherPassword = "ADONET";repl.Publication = "Northwind";repl.Subscriber = "OrderTaker"; repl.SubscriberConnectionString = "Data Source=northwind.sdf";

// Create the Local SSCE Database subscriptionrepl.AddSubscription(AddOption.CreateDatabase);

// Synchronize to the SQL Server 2000 to populate the Subscription repl.Synchronize();

Page 18: Data Access Mobile Devices

SQL Server CE – Merge Replication (2)

SQL Server CE – Merge Replication (2)

Local access is identical to RDA– Uses same local database– And same data access classes

Local access is identical to RDA– Uses same local database– And same data access classes

// Create the connection and commandSqlCeConnection conn = new SqlCeConnection("...");SqlCeCommand cmd = conn.CreateCommand();cmd.CommandText = "SELECT * FROM Customers";

// Create a DataSet to put our results inDataSet ds = new DataSet();

// Create the adapterSqlCeDataAdapter da = new SqlCeDataAdapter(cmd);

// Fill itda.Fill(ds, "Customers");

Page 19: Data Access Mobile Devices

SQL Server CE – Merge Replication (3)

SQL Server CE – Merge Replication (3)

Merges back to remote db with Replication– Uses same replication object as filled

local db– Synchronize uses Merge Replication

Merges back to remote db with Replication– Uses same replication object as filled

local db– Synchronize uses Merge ReplicationSqlCeReplication repl = new SqlCeReplication();

// ...

// Synchronize to the SQL Server 2000 // to populate the Subscription repl.Synchronize();

Page 20: Data Access Mobile Devices

SQL Server CE - CaveatsSQL Server CE - Caveats

Several SQL Constructs don’t work– Batch queries– Object names with spaces (cannot use

delimiters) SELECT * FROM [Order Details]

– Queries are run with unchangable defaults QUOTED_IDENTIFIER_ON, ANSI_NULLS_ON,

ANSI_PADDING_ON, ANSI_NULL_DFLT_ON_ON, CONCAT_NULL_YIELDS_NULL_ON

Several SQL Constructs don’t work– Batch queries– Object names with spaces (cannot use

delimiters) SELECT * FROM [Order Details]

– Queries are run with unchangable defaults QUOTED_IDENTIFIER_ON, ANSI_NULLS_ON,

ANSI_PADDING_ON, ANSI_NULL_DFLT_ON_ON, CONCAT_NULL_YIELDS_NULL_ON

Page 21: Data Access Mobile Devices

Web ServicesWeb Services

SOAP Based Data Access– Only real data access on SmartPhones– Doesn’t require local connectivity– Use WriteSchema to pass whole structure

of DS– Can use DataSets for updates

SOAP Based Data Access– Only real data access on SmartPhones– Doesn’t require local connectivity– Use WriteSchema to pass whole structure

of DS– Can use DataSets for updates[WebMethod]

public XmlDocument Products() { // ...

// Using WriteSchema to make sure the entire // structure is included on client side MemoryStream strm = new MemoryStream(); ds.WriteXml(strm, XmlWriteMode.WriteSchema); strm.Position = 0; XmlDocument doc = new XmlDocument(); doc.Load(strm);

return doc;}

Page 22: Data Access Mobile Devices

Web Services (2)Web Services (2)

The Client consumes it normally– Can use DataSets on the client for cache– Use ReadSchema to fill in DS Structure

The Client consumes it normally– Can use DataSets on the client for cache– Use ReadSchema to fill in DS Structure

// Create the serviceGetPhoneDataService theSvc = new GetPhoneDataService();

// Load the data through the Web ServiceXmlDocument doc = theSvc.Products();

// NOTE: Can't use a Typed DataSet Here// Make it into a DataSetDataSet ds = new DataSet();XmlNodeReader rdr = new XmlNodeReader(node);ds.ReadXml(rdr, XmlReadMode.ReadSchema);

// Must use AcceptChanges // (ReadXml makes rows new)ds.AcceptChanges();

Page 23: Data Access Mobile Devices

Web Services (3)Web Services (3)

Can store DataSet locally for cache– This allows for the device to be

disconnected– Saving the data locally includes the

changes– Use DiffGrams to preserve the changes

Can store DataSet locally for cache– This allows for the device to be

disconnected– Saving the data locally includes the

changes– Use DiffGrams to preserve the changes// Write the data locally

ds.WriteXml("local.xml", XmlWriteMode.DiffGram);

// Read it locallyds.ReadXml("local.xml", XmlReadMode.DiffGram);

Page 24: Data Access Mobile Devices

Web Services (4)Web Services (4)

Updating happens the same way– Limitations of CF DataSets makes it

chatty– Prefer to use DiffGrams

though whole DS are still sent

Updating happens the same way– Limitations of CF DataSets makes it

chatty– Prefer to use DiffGrams

though whole DS are still sentXmlDocument doc = new XmlDocument();

MemoryStream strm = new MemoryStream();XmlTextWriter writer = new XmlTextWriter(strm, System.Text.Encoding.UTF8);ds.WriteXml(writer, XmlWriteMode.DiffGram);strm.Position = 0;doc.Load(strm);

svc.SetDataSet(doc);

[WebMethod]public XmlDocument SaveChanges(XmlDocument doc) { MyTypedDs ds = new MyTypedDs(); ds.ReadXml(doc, XmlReadMode.DiffGram);

DataSet updated = UpdateDataSet(ds);

return new XmlDataDocument(updated);}

Page 25: Data Access Mobile Devices

DataBindingDataBinding

Performance Issues– DataBinding on devices is very slow

Manually filling-in controls much faster E.g. 2 seconds vs. 450ms

Performance Issues– DataBinding on devices is very slow

Manually filling-in controls much faster E.g. 2 seconds vs. 450ms

// This is fasterforeach (DataRow cust in ds.Tables["Customers"].Rows) { listBox1.Items.Add(cust["CompanyName"]);}

// This is slower, but more functionallistBox1.DataSource = ds.Tables["Customers"];listBox1.DisplayMember = "CompanyName";listBox1.ValueMember = "CustomerID";

Page 26: Data Access Mobile Devices

DataBinding (2)DataBinding (2)

DataBinding is worth it sometimes– If binding multiple items to a single

source– If you need a CurrencyManager– If you need Master/Detail Binding

DataBinding is worth it sometimes– If binding multiple items to a single

source– If you need a CurrencyManager– If you need Master/Detail Binding

CurrencyManager mgr = (CurrencyManager)listBox1.BindingContext[ds.Tables["Customers"]];mgr.Position++;

DataTable tbl = ds.Tables["Customers"];listBox.DataSource = tbl;comboBox.DataSource = tbl;textBox.Bindings.Add("Text", tbl, "CompanyName");

Page 27: Data Access Mobile Devices

DataBinding (3)DataBinding (3)

– Use of background threads can help– Use of background threads can help

// Launch the process on a background thread ThreadPool.QueueUserWorkItem(new WaitCallback(BindDataToForm));

void BindDataToForm(object o) { // Do Data Binding}

Page 28: Data Access Mobile Devices

SQL Server Mobile EditionSQL Server Mobile Edition

What’s New (Replacement for SQL CE)– SQL Server 2005 Integration

API Access to Merge Replication Engine Download only Tables (read-only) Partitioned Articles

– Synchronization Sync Progress API’s Column level Synchronization Variable Compression Levels Multiple Subscriptions per DB Table

What’s New (Replacement for SQL CE)– SQL Server 2005 Integration

API Access to Merge Replication Engine Download only Tables (read-only) Partitioned Articles

– Synchronization Sync Progress API’s Column level Synchronization Variable Compression Levels Multiple Subscriptions per DB Table

Page 29: Data Access Mobile Devices

SQL Server Mobile Edition (2)SQL Server Mobile Edition (2)

What’s New (continued)– Client Storage

Improved ACID Support Improved Query Processor

– Client Performance Multi-User Support (allow sync while in use) SqlCeResultSet (Updatable Cursor)

– Client Support Additional Device Support Including SmartPhone

What’s New (continued)– Client Storage

Improved ACID Support Improved Query Processor

– Client Performance Multi-User Support (allow sync while in use) SqlCeResultSet (Updatable Cursor)

– Client Support Additional Device Support Including SmartPhone

Page 30: Data Access Mobile Devices

Questions?Questions?