Sdílet prostřednictvím


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 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).

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 within 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, a data table on the desktop or the mobile device. You simply use the System.Data.SqlServerCe namespace and classes. For example you can do the following:

The .NET Framework provides DataAdapter to allow the previously mentioned classes to be used together easily. The SqlCeConnection object can be set using the SqlCeDataAdapter object's connection property.

The query to execute is specified using the DataAdapter's SelectCommand property or simply passed to the DataAdapter's constructor, along with the connection object.

da = new SqlCeDataAdapter("SELECT * FROM Users", cn);

The DataTable object is created using the Fill method of the DataAdapter object. The DataTable object contains the result set data returned by the query. You can iterate through the DataTable 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:

For more information about automated conversion from Java to C#, see What's New in Java Language Conversion Assistant 3.0.

Compiling the Code

Before you can work with a SQLCE database from an application, you need to add a reference to System.Data.SqlServerCe to your project. This is accomplished by selecting Add Reference on the Project menu in the development environment. Then, select the System.Data.SqlServerCe component from the Add Reference dialog box.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. 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 called Database.sdf. You can build this table on the device using SQL CE tools, or replicated from an 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

Reference

Smart Devices
SqlConnection
SqlCommand

Concepts

C# Programming Guide
ADO.NET DataSet

Other Resources

C# for Java Developers
Smart Device Development
Using DataSets in ADO.NET
ADO.NET for the Java Programmer