Data Access Mobile Devices
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,692
On Slideshare
4,678
From Embeds
14
Number of Embeds
1

Actions

Shares
Downloads
35
Comments
0
Likes
1

Embeds 14

http://www.slideshare.net 14

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Mobile Data with the Compact Framework Shawn Wildermuth Senior Consultant/Architect Magenic Technologies
  • 2. Who I Am
    • Shawn Wildermuth (swildermuth@adoguy.com)
    • C# MVP
    • INETA Speaker
    • Book Author
      • “ Pragmatic ADO.NET”
    • Editor of http://ONDotnet.com
    • This Presentation can be found at:
      • http://adoguy.com/presentations
  • 3. Agenda
    • 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)
  • 4. How 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
  • 5. Deciding 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
  • 6. Limitations 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
  • 7. 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)
  • 8. Limitations of the CF (3)
    • DataSet is limited
      • Merge() and GetChanges() are missing
      • Deal with changes row-by-row
  • 9. SQL Client Data Access
    • Remarkable similar to SqlClient in Full Fx
      • Full Set of Adapter Functionality Supported
      • Connection Strings are to real SQL Server
    // Create the connection and command SqlConnection conn = new SqlConnection("..."); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Customers"; // 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, "Customers");
  • 10. SQL Client Data Access (2)
    • Readers are also supported
      • Same caveats apply to Mobile Apps
      • Connected apps are hard on low bandwidth
    // Create the connection and command SqlConnection conn = new SqlConnection("..."); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Customers"; // Use a Reader try { conn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { string name = rdr["CompanyName"].ToString(); } } finally { conn.Close(); }
  • 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. 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
  • 13. Setting 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
    SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(&quot;http://shawnw-lptd/sqlce/sscesa20.dll&quot;, &quot;Data Source=northwind.sdf&quot;);
  • 14. SQL 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
    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. SQL Server CE – RDA (2)
    • Uses local database storage
      • Use of SqlCe* classes to access data normally
      • Supports full suite of classes
    // 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. 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
    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. SQL 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
    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. SQL Server CE – Merge Replication (2)
    • Local access is identical to RDA
      • Uses same local database
      • And same data access classes
    // 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. 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
    SqlCeReplication repl = new SqlCeReplication(); // ... // Synchronize to the SQL Server 2000 // to populate the Subscription repl.Synchronize();
  • 20. SQL 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
  • 21. Web 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
    [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. Web Services (2)
    • The Client consumes it normally
      • Can use DataSets on the client for cache
      • Use ReadSchema to fill in DS Structure
    // 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. 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
    // Write the data locally ds.WriteXml(&quot;local.xml&quot;, XmlWriteMode.DiffGram); // Read it locally ds.ReadXml(&quot;local.xml&quot;, XmlReadMode.DiffGram);
  • 24. 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
    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. DataBinding
    • Performance Issues
      • DataBinding on devices is very slow
        • Manually filling-in controls much faster
        • E.g. 2 seconds vs. 450ms
    // 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. 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
    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. DataBinding (3)
      • 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 }
  • 28. SQL 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
  • 29. 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
  • 30. Questions?