Welcome!

.NET Authors: Elizabeth White, Patrick Burke, Maureen O'Gara

Related Topics: .NET

.NET: Article

Have PDA, Will Travel

Have PDA, Will Travel

In order to host quality mobile applications, smart devices require characteristics that include being small and portable, having a decent battery life, displaying information with richness and quality, having memory to hold plenty of information and applications, and the ability to access remote data. In this column, we'll introduce the topic of accessing remote data using the .NET Compact Framework by focusing on just one of the several ways in which remote information can be manipulated.

Data in a Connected World
Everyone opens up their first PDA like it's Christmas morning. It's easy to enjoy the coolness of being able to carry around a device that contains a considerable amount of information and to use an application that will access that data. But we soon discover that visiting the cradle periodically is required for the device to be updated with current information. This type of synchronization requires tethering to a PC and is therefore unwieldy in many situations. In today's business world, being successful requires that companies become increasingly more agile. This includes being sensitive to changing market factors and understanding changing customer needs. The companies that are quicker to change usually live longer and wealthier. Therefore, all sorts of company information has to be available and accessible anytime and anywhere. Consequently, it is not just a stand-alone PDA environment anymore. Corporate applications need to get on mobile or smart devices, and the devices absolutely require the ability to communicate to enterprise back ends in an easy fashion.

The integration of mobile devices, the Internet, and wireless connectivity provides a way for companies to extend their reach to employees, partners, and clients. The potential impact of this convergence is unlimited. Possible results for each of the groups include improved productivity, reduced operational costs, and increased satisfaction for all involved. The geographic expansion of wireless connectivity over the past few years has created a situation in which a device can retrieve information from anywhere in the world.

It turns out that the Compact Framework provides a great deal of assistance in accessing data remotely, including high-level communication mechanisms for accessing SQL Server and XML Web services. Furthermore, a Compact Framework-based application can work at different levels of the network stack, including TCP/IP, Sockets and Infrared, Streams, and HTTP. In this column we'll focus only on accessing a remote SQL Server, but we cover this entire group and other communication issues in our upcoming book.

ADO.NET and the Compact Framework
One way to remotely access data is to use classes from the System.Data.SqlClient namespace for working with SQL Server.

SQL Server is one of the leading database servers today and is therefore familiar to most desktop and server programmers who work with the .NET Framework. SQL Server is also well known to those who worked in the ADO environments (VB 6.0 and ASP) of yesterday. Moreover, many developers interested in the Compact Framework are in offices that use Microsoft technologies and therefore probably have SQL Server databases implemented in important applications. So, a normal transition to smart device architectures will be to access these already-established data sources.

As many developers will already be aware, ADO.NET is the technology that .NET developers use for accessing data in data sources like database servers. Rooted in the System.Data namespace, ADO.NET provides a model that does several things for the developer, including the combination of relational and object-oriented paradigms, integration with XML, and the abstraction of database server protocols.

There are several benefits to the way ADO.NET defines data providers. These benefits emanate from the fact that the class model or architecture used (the base classes and interfaces) in the .NET Framework is the same as for the Compact Framework. Therefore, the knowledge of providers in the .NET Framework will be mostly reused, except for the actual database implementation differences and smart device constraint considerations. The main benefit that flows from this architecture is that Compact Framework SqlClient is identical to the .NET Framework implementation of SqlClient. A second benefit is that other data providers can be built in the Compact Framework that are more easily understood since they are based on a common architecture. In the case of the Compact Framework, the SQL Server 2000 Windows CE Edition (SQLCE) team provided a rich data access mechanism in System.Data.SqlServerCe, a data provider for SQLCE that is a great database residing on Windows CE devices.

Because ADO.NET Data Providers are based on a common architecture, they are required to implement certain interfaces. Therefore, all providers will have managed classes for Connection, Commands, Parameters, Data Adapter, Command Builder, Data Reader, Transaction, Exceptions, and more. Missing from this is the DataSet, as DataSets are provider independent.

Enter the SqlClient Provider
The easiest way to exchange data with SQL Server is to use the SqlClient provider, whether on a PC or on a smart device. As stated earlier, the usage of SqlClient on a smart device is the same as the usage on the .NET Framework.

OleDb and Odbc are "broad" data providers in the .NET Framework, and as mentioned in last month's column, they do not exist in the Compact Framework. They are considered "broad providers" because they serve as an interface for multiple database vendors and cannot take advantage of any certain database capability. Thus, broad providers cannot create a performance gain based on the strength of the database. As SqlClient is a narrow provider (i.e., it is only intended to work with SQL Server databases), it has no excess layers of communication code and is therefore the fastest way to interact with SQL Server.

Most developers who develop applications that use SqlClient do not realize what is happening behind the scenes when a client machine talks to another SQL Server database machine. This is due in part to ADO.NET acting as an abstraction layer so developers can focus on their application. It turns out that SqlClient is using the Tabular Data Stream (TDS) wire protocol that is specific to SQL Server. TDS is provided through a SQL Server client Net-Library that is installed as a part of MDAC for PCs, and for smart devices is automatically added to the smart device project deployment in Visual Studio .NET. The classes in SqlClient take care of converting the query or command to the database into the TDS packets and shipping to the server. They also take care of the response.

Figure 1 is a diagram of the SqlClient namespace. Because of the ADO.NET data provider architecture, certain interfaces and base classes are required to be implemented and this illustration depicts that fact through the set of classes prefixed with "Sql". Again, this is the same SqlClient model for the .NET Framework and the Compact Framework. Since information on the programming details of ADO.NET and SqlClient is widespread and can be applied to the usage in the Compact Framework, we will not cover every class in the figure.

 

SqlClient on a Smart Device
There are several issues to consider when deciding whether to use SqlClient on a smart device.

The first issue to address is to contrast the usage of SqlClient and SQLCE. Although SQLCE has many advantages, especially for disconnected and occasionally connected scenarios, it is important to understand that there are times when using SQL Server remotely is a good choice.

For example, consider the following scenario. The device is capturing data in a manufacturing environment and the back-end application is exposed by a SQL Server 2000 database. The business requires the captured data to be placed in the SQL Server database as soon as possible. In most cases, this database exists due to an existing Windows application. If the device can maintain a full-time high-speed connection to the local network, this scenario works great. And we see this in data-collection environments, like manufacturing, warehousing, and hospitality, in which the devices are typically used within a circumscribed environment like a shop floor or warehouse.

A second scenario is where the data is too large for the smart device, but complex queries can be made. This still allows for sending requests to a rich query system provided by SQL Server, and also allows for the device to maintain a low memory footprint. Again, this is for special environments in which the device maintains a reliable connection to the network especially in times of communication with the database.

Even though there are benefits in the SqlClient, there are some limitations to consider if using the Compact Framework implementation of SqlClient:

  • Mobility is limited due to dependence on a full-time, high-speed, reliable network connection.
  • For scenarios in which devices access the the Internet, the SQL Server has to be exposed to the Internet. Consequently, well-documented firewall ports will have to be opened. There are options to change to other ports besides the default 1433 port.
  • Automatic port discovery is not supported on the device. SqlClient has to have the port number defined in the connection string in scenarios of a named server instance not using the default port 1433.
  • In applications where there are periods of no connectivity, there will be some additional programming effort. The downloaded data will have to be cached (which is not difficult using a DataSet). And when the time comes to update SQL Server, a connection will have to be made and transmission code executed. These are simple tasks, but the plumbing around good synchronization takes additional effort. Synchronization is not only just the act of exchanging data but also providing for error recovery and other related issues.
  • Since the client is connecting directly to the database, the scalability of the application may be reduced because this is a two-tier architecture.
  • Encryption of the communication channel is not supported.
  • Even though the SqlClient in the .NET Framework has several different protocols (TCP/IP, Named Pipes, AppleTalk, Banyan, NWLink, IPX/SPX, and VIA) due to the underlying Net-Library, the SqlClient in the Compact Framework only supports TCP/IP. Therefore, the SQL Server has to have this protocol enabled.
  • There is no connection pooling on a smart device.

    SqlClient also allows for Windows account credentials to be passed in the connection string, thus allowing the use of Windows authentication in addition to SQL Security on the database server. To do this, the developer has to use the "Integrated Security" parameter and also include the "User ID" and "Password" entries as well.

    Conclusion
    SqlClient is a well-known data provider for those who already use SQL Server while developing for the .NET Framework. However, it should be used in the right situations. Considerations include scenarios where there is the need to access data that is larger than the smart device can store or the data does not require future updates or searching. The scenario would further require the environment to have connectivity that is full time, reliable, and high-speed. Due to these requirements, the SqlClient provider is not recommended for most occasionally connected or disconnected applications.

    For such scenarios there are better alternatives. XML Web services might be considered a better architecture since the application will gain scalability due to accessing the middle tier, but still requires custom synchronization code. The premier disconnected architecture will utilize SQLCE and the SqlServerCe namespace. This mechanism supports replication and works well with firewalls.

    For more on SQL Server CE 2.0 and the .NET Compact Framework, see "Accessing Enterprise Data from SQL Server CE," by Chris D. Mayo and Jon Rauschenberger on page 14.

  • More Stories By Jon Box

    Jon Box is an Architect Evangelist in Developer & Platform Evangelism with the Microsoft Corporation. He coauthored Building Solutions with the Microsoft .NET Compact Framework, published by Addison-Wesley, and blogs at http://blogs.msdn.com/jonbox/default.aspx.

    Comments (3) View Comments

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    Most Recent Comments
    Jon Box 07/01/03 04:54:00 PM EDT

    .NET does have a server side solution to supporting Palm. And that's by developing ASP.NET mobile control applications.

    Greg Saddler 06/18/03 01:07:00 AM EDT

    Agree with "Palm would be nicer" and infinitely more interesting and useful. When is Microsoft gonna grow up from their self-centered world and start selling solutions instead of technology. The biggest force advancing computing has been sharing and cooperation made possible by the Internet, neither is Microsoft known for. Microsoft is like the spoiled kid at the court who won't play or share unless everyone uses his ball and plays by his rules. Just think how much further along the computing platform would be if Bill and Larry had both finished college and learned about... how to accept another point of view.

    Peter Horwood 06/17/03 05:33:00 PM EDT

    This was interesting, but really, I don't care about the high battery use CE devices. I and my clients either use real computers (desk or laptops) or Palm's. I'd LOVE a way to do PALM development/integration easily from my .NET app's. THAT would be VERY interisting - and useful!