Data Access Mobile Devices


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Data Access Mobile Devices

  1. 1. Mobile Data with the Compact Framework Shawn Wildermuth Senior Consultant/Architect Magenic Technologies
  2. 2. Who I Am <ul><li>Shawn Wildermuth ( </li></ul><ul><li>C# MVP </li></ul><ul><li>INETA Speaker </li></ul><ul><li>Book Author </li></ul><ul><ul><li>“ Pragmatic ADO.NET” </li></ul></ul><ul><li>Editor of </li></ul><ul><li>This Presentation can be found at: </li></ul><ul><ul><li> </li></ul></ul>
  3. 3. Agenda <ul><li>How Mobile Data is Different </li></ul><ul><li>Deciding on a Solution </li></ul><ul><li>Limitations of the Compact Framework </li></ul><ul><li>Using SqlClient </li></ul><ul><li>Using SQL Server CE </li></ul><ul><li>Using Web Services </li></ul><ul><li>Data Binding on the Compact Framework </li></ul><ul><li>SQL Server CE 2005 (Yukon) </li></ul>
  4. 4. How Mobile Data is Different <ul><li>The form factor is small </li></ul><ul><ul><li>Not much screen real estate </li></ul></ul><ul><ul><li>Not much memory room </li></ul></ul><ul><li>Needs to work disconnected </li></ul><ul><ul><li>Very few Mobile devices are always connected </li></ul></ul><ul><ul><li>Smaller bandwidth usually when connected </li></ul></ul>
  5. 5. Deciding on a Solution <ul><li>Is a Smart Client the right solution? </li></ul><ul><ul><li>How much data manipulation is needed? </li></ul></ul><ul><ul><li>Is offline support needed? </li></ul></ul><ul><ul><li>Does state need to be shared with servers </li></ul></ul><ul><li>If Smart Client, which Data Access? </li></ul><ul><ul><li>Options include: </li></ul></ul><ul><ul><ul><li>SQL Server Data Access </li></ul></ul></ul><ul><ul><ul><li>SQL Server CE Data Access </li></ul></ul></ul><ul><ul><ul><li>Web Services </li></ul></ul></ul>
  6. 6. Limitations of the CF <ul><li>System.Data </li></ul><ul><ul><li>DataSets work fine </li></ul></ul><ul><ul><li>Typed DataSets don’t </li></ul></ul><ul><ul><li>No Design-time Support </li></ul></ul><ul><li>System.Xml </li></ul><ul><ul><li>No XML Serialization Support </li></ul></ul><ul><ul><li>OpenNETCF’s Xml Serialization isn’t Complete </li></ul></ul><ul><li>System.Runtime.Serialization </li></ul><ul><ul><li>No SOAP or Binary Formatters </li></ul></ul>
  7. 7. Limitations of the CF (2) <ul><li>SqlClient </li></ul><ul><ul><li>Only on CE and Pocket PC (no SmartPhone) </li></ul></ul><ul><ul><li>Requires Connectivity </li></ul></ul><ul><li>SQL CE </li></ul><ul><ul><li>Only on CE and Pocket PC (no SmartPhone) </li></ul></ul><ul><li>Web Services </li></ul><ul><ul><li>Support on all platforms </li></ul></ul><ul><ul><li>Somewhat slower (not compressed like above) </li></ul></ul>
  8. 8. Limitations of the CF (3) <ul><li>DataSet is limited </li></ul><ul><ul><li>Merge() and GetChanges() are missing </li></ul></ul><ul><ul><li>Deal with changes row-by-row </li></ul></ul>
  9. 9. SQL Client Data Access <ul><li>Remarkable similar to SqlClient in Full Fx </li></ul><ul><ul><li>Full Set of Adapter Functionality Supported </li></ul></ul><ul><ul><li>Connection Strings are to real SQL Server </li></ul></ul>// Create the connection and command SqlConnection conn = new SqlConnection(&quot;...&quot;); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = &quot;SELECT * FROM Customers&quot;; // Create a DataSet to put our results in DataSet ds = new DataSet(); // Create the adapter SqlDataAdapter da = new SqlDataAdapter(cmd); // Fill it da.Fill(ds, &quot;Customers&quot;);
  10. 10. SQL Client Data Access (2) <ul><li>Readers are also supported </li></ul><ul><ul><li>Same caveats apply to Mobile Apps </li></ul></ul><ul><ul><li>Connected apps are hard on low bandwidth </li></ul></ul>// Create the connection and command SqlConnection conn = new SqlConnection(&quot;...&quot;); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = &quot;SELECT * FROM Customers&quot;; // Use a Reader try { conn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { string name = rdr[&quot;CompanyName&quot;].ToString(); } } finally { conn.Close(); }
  11. 11. SQL Server CE CLIENT SERVER QP/Cursor Engine/ES Storage Engine / Repl Tracking SQL CE Edition v2.0 OLEDB OLEDB OLEDB CE CLR / .NET CF SQL Server CE Data Provider ADO.NET VS .NET (VB.NET, C#) . NET CF / Managed Stack IIS Server Agent: Replication and Remote Data Access HTTP 802.11b, CDPD, GSM, CDMA, TDMA, etc. Occasionally Connected Data Provider Client Agent: Replication and RDA
  12. 12. SQL Server CE (2) <ul><li>Two Methodologies </li></ul><ul><ul><li>Remote Data Access </li></ul></ul><ul><ul><ul><li>Local Database </li></ul></ul></ul><ul><ul><ul><li>Push and Pull Access from SQL Server </li></ul></ul></ul><ul><ul><ul><li>Optional Change Tracking </li></ul></ul></ul><ul><ul><li>Merge Replication </li></ul></ul><ul><ul><ul><li>Use Replication Engine to Sync Changes </li></ul></ul></ul><ul><ul><ul><li>Use Subscription model instead of RDA Model </li></ul></ul></ul><ul><ul><ul><li>Allows vertical and horizontal partitioning </li></ul></ul></ul>
  13. 13. Setting up SQL Server CE - RDA <ul><li>Integrates with IIS </li></ul><ul><ul><li>ISAPI Application to access remote data </li></ul></ul><ul><ul><ul><li>E.g. http://<machinename>/sqlce/sscesa20.dll </li></ul></ul></ul><ul><ul><ul><li>Compresses results across the wire </li></ul></ul></ul><ul><ul><li>SqlCeRemoteDataAccess </li></ul></ul><ul><ul><ul><li>Takes an URL to the ISAPI App; </li></ul></ul></ul><ul><ul><ul><li>and Connection String to database on mobile device </li></ul></ul></ul>SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(&quot;http://shawnw-lptd/sqlce/sscesa20.dll&quot;, &quot;Data Source=northwind.sdf&quot;);
  14. 14. SQL Server CE - RDA <ul><li>Pulling Data to Mobile Device </li></ul><ul><ul><li>Creation of the local DB with ad-hoc queries </li></ul></ul><ul><ul><li>Uses an OLEDB Connection String to connect </li></ul></ul><ul><ul><li>Pull to bring data from remote db to mobile db </li></ul></ul><ul><ul><li>Can include tracking information for updating </li></ul></ul>string rdaOleDbConnectString = &quot;Provider=sqloledb;Data Source=shawnw-lptd;&quot; + &quot;Initial Catalog=Northwind;User Id=Sample;Password=ADONET&quot;; rda.Pull(&quot;Customers&quot;, &quot;SELECT * FROM Customers&quot;, rdaOleDbConnectString , RdaTrackOption.TrackingOnWithIndexes); rda.Pull(&quot;Products&quot;, &quot;SELECT * FROM Products&quot;, rdaOleDbConnectString , RdaTrackOption.TrackingOnWithIndexes); rda.Pull(&quot;Orders&quot;, &quot;SELECT * FROM Orders&quot;, rdaOleDbConnectString , RdaTrackOption.TrackingOnWithIndexes); rda.Pull(&quot;OrderDetails&quot;, &quot;SELECT * FROM OrderDetails&quot;, rdaOleDbConnectString , RdaTrackOption.TrackingOnWithIndexes);
  15. 15. SQL Server CE – RDA (2) <ul><li>Uses local database storage </li></ul><ul><ul><li>Use of SqlCe* classes to access data normally </li></ul></ul><ul><ul><li>Supports full suite of classes </li></ul></ul>// Create the connection and command Sql Ce Connection conn = new Sql Ce Connection(&quot;...&quot;); Sql Ce Command cmd = conn.CreateCommand(); cmd.CommandText = &quot;SELECT * FROM Customers&quot;; // Create a DataSet to put our results in DataSet ds = new DataSet(); // Create the adapter Sql Ce DataAdapter da = new Sql Ce DataAdapter(cmd); // Fill it da.Fill(ds, &quot;Customers&quot;);
  16. 16. SQL Server CE – RDA (3) <ul><li>Pushing data back </li></ul><ul><ul><li>Pushes changed rows back to remote DB </li></ul></ul><ul><ul><li>Only if tracking is enabled </li></ul></ul><ul><ul><li>Batching does all rows in single Tx </li></ul></ul>rda.Push(&quot;Customers&quot;, rdaOleDbConnectString, RdaBatchOption.BatchingOn); rda.Push(&quot;Products&quot;, rdaOleDbConnectString, RdaBatchOption.BatchingOn); rda.Push(&quot;Orders&quot;, rdaOleDbConnectString, RdaBatchOption.BatchingOn); rda.Push(&quot;OrderDetails&quot;, rdaOleDbConnectString, RdaBatchOption.BatchingOn);
  17. 17. SQL Server CE – Merge Replication <ul><li>Retrieves Data with Replication </li></ul><ul><ul><li>Fills the local DB from replication subscription </li></ul></ul><ul><ul><li>Like RDA, uses ISAPI Application </li></ul></ul><ul><ul><li>And local database connection for local cache </li></ul></ul>SqlCeReplication repl = new SqlCeReplication(); repl.InternetUrl = &quot;http://shawnw-lptd/sqlce/sscesa20.dll&quot;; repl.Publisher = &quot;SHAWNW-LPTD&quot;; repl.PublisherDatabase = &quot;Northwind&quot;; repl.PublisherLogin = &quot;sample&quot;; repl.PublisherPassword = &quot;ADONET&quot;; repl.Publication = &quot;Northwind&quot;; repl.Subscriber = &quot;OrderTaker&quot;; repl.SubscriberConnectionString = &quot;Data Source=northwind.sdf&quot;; // Create the Local SSCE Database subscription repl.AddSubscription(AddOption.CreateDatabase); // Synchronize to the SQL Server 2000 to populate the Subscription repl.Synchronize();
  18. 18. SQL Server CE – Merge Replication (2) <ul><li>Local access is identical to RDA </li></ul><ul><ul><li>Uses same local database </li></ul></ul><ul><ul><li>And same data access classes </li></ul></ul>// Create the connection and command SqlCeConnection conn = new SqlCeConnection(&quot;...&quot;); SqlCeCommand cmd = conn.CreateCommand(); cmd.CommandText = &quot;SELECT * FROM Customers&quot;; // Create a DataSet to put our results in DataSet ds = new DataSet(); // Create the adapter SqlCeDataAdapter da = new SqlCeDataAdapter(cmd); // Fill it da.Fill(ds, &quot;Customers&quot;);
  19. 19. SQL Server CE – Merge Replication (3) <ul><li>Merges back to remote db with Replication </li></ul><ul><ul><li>Uses same replication object as filled local db </li></ul></ul><ul><ul><li>Synchronize uses Merge Replication </li></ul></ul>SqlCeReplication repl = new SqlCeReplication(); // ... // Synchronize to the SQL Server 2000 // to populate the Subscription repl.Synchronize();
  20. 20. SQL Server CE - Caveats <ul><li>Several SQL Constructs don’t work </li></ul><ul><ul><li>Batch queries </li></ul></ul><ul><ul><li>Object names with spaces (cannot use delimiters) </li></ul></ul><ul><ul><ul><li>SELECT * FROM [Order Details] </li></ul></ul></ul><ul><ul><li>Queries are run with unchangable defaults </li></ul></ul><ul><ul><ul><li>QUOTED_IDENTIFIER_ON, ANSI_NULLS_ON, ANSI_PADDING_ON, ANSI_NULL_DFLT_ON_ON, CONCAT_NULL_YIELDS_NULL_ON </li></ul></ul></ul>
  21. 21. Web Services <ul><li>SOAP Based Data Access </li></ul><ul><ul><li>Only real data access on SmartPhones </li></ul></ul><ul><ul><li>Doesn’t require local connectivity </li></ul></ul><ul><ul><li>Use WriteSchema to pass whole structure of DS </li></ul></ul><ul><ul><li>Can use DataSets for updates </li></ul></ul>[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; }
  22. 22. Web Services (2) <ul><li>The Client consumes it normally </li></ul><ul><ul><li>Can use DataSets on the client for cache </li></ul></ul><ul><ul><li>Use ReadSchema to fill in DS Structure </li></ul></ul>// Create the service GetPhoneDataService theSvc = new GetPhoneDataService(); // Load the data through the Web Service XmlDocument doc = theSvc.Products(); // NOTE: Can't use a Typed DataSet Here // Make it into a DataSet DataSet ds = new DataSet(); XmlNodeReader rdr = new XmlNodeReader(node); ds.ReadXml(rdr, XmlReadMode.ReadSchema); // Must use AcceptChanges // (ReadXml makes rows new) ds.AcceptChanges();
  23. 23. Web Services (3) <ul><li>Can store DataSet locally for cache </li></ul><ul><ul><li>This allows for the device to be disconnected </li></ul></ul><ul><ul><li>Saving the data locally includes the changes </li></ul></ul><ul><ul><li>Use DiffGrams to preserve the changes </li></ul></ul>// Write the data locally ds.WriteXml(&quot;local.xml&quot;, XmlWriteMode.DiffGram); // Read it locally ds.ReadXml(&quot;local.xml&quot;, XmlReadMode.DiffGram);
  24. 24. Web Services (4) <ul><li>Updating happens the same way </li></ul><ul><ul><li>Limitations of CF DataSets makes it chatty </li></ul></ul><ul><ul><li>Prefer to use DiffGrams </li></ul></ul><ul><ul><ul><li>though whole DS are still sent </li></ul></ul></ul>XmlDocument 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); }
  25. 25. DataBinding <ul><li>Performance Issues </li></ul><ul><ul><li>DataBinding on devices is very slow </li></ul></ul><ul><ul><ul><li>Manually filling-in controls much faster </li></ul></ul></ul><ul><ul><ul><li>E.g. 2 seconds vs. 450ms </li></ul></ul></ul>// This is faster foreach (DataRow cust in ds.Tables[&quot;Customers&quot;].Rows) { listBox1.Items.Add(cust[&quot;CompanyName&quot;]); } // This is slower, but more functional listBox1.DataSource = ds.Tables[&quot;Customers&quot;]; listBox1.DisplayMember = &quot;CompanyName&quot;; listBox1.ValueMember = &quot;CustomerID&quot;;
  26. 26. DataBinding (2) <ul><li>DataBinding is worth it sometimes </li></ul><ul><ul><li>If binding multiple items to a single source </li></ul></ul><ul><ul><li>If you need a CurrencyManager </li></ul></ul><ul><ul><li>If you need Master/Detail Binding </li></ul></ul>CurrencyManager mgr = (CurrencyManager)listBox1.BindingContext[ds.Tables[&quot;Customers&quot;]]; mgr.Position++; DataTable tbl = ds.Tables[&quot;Customers&quot;]; listBox.DataSource = tbl; comboBox.DataSource = tbl; textBox.Bindings.Add(&quot;Text&quot;, tbl, &quot;CompanyName&quot;);
  27. 27. DataBinding (3) <ul><ul><li>Use of background threads can help </li></ul></ul>// Launch the process on a background thread ThreadPool.QueueUserWorkItem(new WaitCallback(BindDataToForm)); void BindDataToForm(object o) { // Do Data Binding }
  28. 28. SQL Server Mobile Edition <ul><li>What’s New (Replacement for SQL CE) </li></ul><ul><ul><li>SQL Server 2005 Integration </li></ul></ul><ul><ul><ul><li>API Access to Merge Replication Engine </li></ul></ul></ul><ul><ul><ul><li>Download only Tables (read-only) </li></ul></ul></ul><ul><ul><ul><li>Partitioned Articles </li></ul></ul></ul><ul><ul><li>Synchronization </li></ul></ul><ul><ul><ul><li>Sync Progress API’s </li></ul></ul></ul><ul><ul><ul><li>Column level Synchronization </li></ul></ul></ul><ul><ul><ul><li>Variable Compression Levels </li></ul></ul></ul><ul><ul><ul><li>Multiple Subscriptions per DB Table </li></ul></ul></ul>
  29. 29. SQL Server Mobile Edition (2) <ul><li>What’s New (continued) </li></ul><ul><ul><li>Client Storage </li></ul></ul><ul><ul><ul><li>Improved ACID Support </li></ul></ul></ul><ul><ul><ul><li>Improved Query Processor </li></ul></ul></ul><ul><ul><li>Client Performance </li></ul></ul><ul><ul><ul><li>Multi-User Support (allow sync while in use) </li></ul></ul></ul><ul><ul><ul><li>SqlCeResultSet (Updatable Cursor) </li></ul></ul></ul><ul><ul><li>Client Support </li></ul></ul><ul><ul><ul><li>Additional Device Support </li></ul></ul></ul><ul><ul><ul><li>Including SmartPhone </li></ul></ul></ul>
  30. 30. Questions?