Mobile Devices and Data (C# vs Java)
With C# and the .NET Compact Framework, you can access and manage database data on a mobile device by using the same concepts and similar APIs that you would use for desktop database programming. On mobile devices, ADO.NET provides a subset of the desktop API targeting Windows CE devices including Pocket PC and Smartphones. For more information, see Database Access (C# vs Java) and Using SQL Server Compact 3.5 Databases (Devices).
Java
In Java you can use J2ME and JDBC to access a database from a mobile device. For more information, see Database Access (C# vs Java). J2ME does not represent a single API on all devices and does not have a single development environment. In addition, J2ME has to run in a Virtual Machine, either KVM or JVM depending on configuration.
C#
In C#, to perform a database read operation, you can use the familiar concepts of a connection, a command, and a data table on the desktop or the mobile device. You use the System.Data.SqlServerCe namespace and classes. For example you can do the following:
Use System.Data.SqlServerCe.SqlCeConnection for the database connection.
Use System.Data.SqlServerCe.SqlCeCommand for your SQL command object.
Use a result set object such as a DataTable for your data table object.
The .NET Framework provides DataAdapter to enable you to use the previously mentioned classes together easily. The System.Data.SqlServerCe.SqlCeConnection object can be set using the System.Data.SqlServerCe.SqlCeDataAdapter connection property of the object.
The query to execute is specified by using the System.Data.SqlServerCe.SqlCeDataAdapter.SelectCommand property of the DataAdapter or the query is passed to the constructor of the DataAdapter together with the connection object.
da = new SqlCeDataAdapter("SELECT * FROM Users", cn);
The DataTable object is created by using the Fill method of the DataAdapter object. The DataAdapter object contains the result set data returned by the query. You can iterate through the DataAdapter object to access the data rows using Rows collection.
The following code illustrates how to access the rows of a table in a SQL Server CE (SQLCE) database on a mobile device.
namespace DataAccessCE
{
using System.Data;
using System.Data.SqlServerCe;
class DataAccessCE
{
public static string connectionString = "";
public static SqlCeConnection cn = null;
public static SqlCeDataAdapter da = null;
public static DataTable dt = new DataTable();
static void Main()
{
connectionString = "Data Source=\\My Documents\\Database.sdf" ;
cn = new SqlCeConnection(connectionString);
da = new SqlCeDataAdapter("SELECT * FROM Users", cn);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
System.Console.WriteLine(dr[0]);
}
}
}
}
For more information, see the following topics:
Compiling the Code
Before you can work with a SQLCE database from an application, you must add a reference to System.Data.SqlServerCe to your project. This is accomplished by clicking Add Reference on the Project menu in the development environment. Then, select the System.Data.SqlServerCe component from the Add Reference dialog box.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
Robust Programming
To compile and run the code, you need the following; otherwise, the line da.Fill(dt); fails and throws an exception.
SQL Server CE installed on the device.
A database table with some data present for testing on an SQLCE database named Database.sdf. You can build this table on the device by using SQL CE tools, or replicate it from a SQL Server desktop to generate the .sdf file. You can add the .sdf file to your project or manually copy it to the directory specified in the connection string.
See Also
Concepts
Reference
Smart Devices (How Do I in C#)