Building Smart Device Applications (SQL Server Compact)

In this section, you will learn how to create a new smart device project in Microsoft Visual Studio, add Microsoft SQL Server Compact 3.5 (SQL Server Compact 3.5) functionality to the project, and then create a new SQL Server Compact 3.5 database by using the System.Data.SqlServerCe namespace. Managed reference topics for the System.Data.SqlServerCe namespace are available in the .NET Framework Reference Documentation.

Using the .NET Compact Framework

The Microsoft .NET Compact Framework provides a way of quickly developing applications for devices. The .NET Compact Framework has two main components:

  • The common language runtime
    The common language runtime is the environment responsible for managing code while the application is running by providing the core services of thread and memory management. Code that targets the runtime is referred to as managed code, while code that does not use the runtime is referred to as unmanaged, or native, code. Native applications for smart devices are built using Microsoft Visual C++ for Devices, while managed applications are built using one of the .NET programming languages, including Microsoft Visual Basic and Microsoft Visual C#.
  • The .NET Compact Framework class library
    The .NET Compact Framework class library provides reusable classes that reduce development time and simplify many common programming tasks, from UI development to thread management to database access.

Note   To develop applications that target the .NET Compact Framework, you must install Microsoft Visual Studio 2003 or a later edition. Within Visual Studio, you can develop applications in either Visual C#, Visual Basic, or both.

Using the SQL Server Data Providers

When you access SQL Server Compact 3.5 databases, you use the System.Data.SqlServerCe namespace. This namespace enables you to do the following in a SQL Server Compact 3.5 database:

  • Access and change data.
  • Administer the database.
  • Synchronize data.

For more information, see Managed Data Provider (SQL Server Compact).

When you access SQL Server databases from a smart device application, you use the .NET Compact Framework data provider for SQL Server. For more information, see .NET Compact Framework Data Provider for SQL Server (SQL Server Compact).

Creating a Smart Device Project

You must first create the project in Visual Studio.

To create a new smart device project

  1. In Visual Studio, on the File menu, point to New, and then select Project.

  2. In the Project Types list of the New Project dialog box, expand the programming language you will use, and then select Smart Device.

  3. In the Templates list, select the type of project you want to create. For example, if you are developing a Pocket PC 2003 application, select Pocket PC 2003 Application.

  4. Provide a name and location for your project, and then click OK.

    Visual Studio creates a new project and displays the main form (Form1). The display will look similar to the operating system you chose.

Adding SQL Server Compact 3.5 to the Project

The next step in building a SQL Server Compact 3.5-enabled application is to add a reference to the SQL Server Compact assembly.

To add a reference to SQL Server Compact 3.5

  1. In Solution Explorer, right-click References and choose Add Reference.

    Note

    If the References folder is not listed in Solution Explorer, click Show All Files at the top of Solution Explorer.

  2. In the list of .NET assemblies, select System.Data.SqlServerCe, and then click OK. If System.Data.SqlServerCe is not listed, follow these steps:

    1. Click Browse.
    2. Locate the following directory:
      C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
    3. Select System.Data.SqlServerCe.dll, and then click OK.

    The list of references in Solution Explorer now includes System.Data.SqlServerCe, and your project can use this assembly.

  3. In Solution Explorer, right-click Form1.cs or Form1.vb and choose View Code.

  4. At the top of the code for the form, add a directive to use the System.Data.SqlServerCe namespace:

    using System.Data.SqlServerCe;
    
    Imports System.Data.SqlServerCe
    

Using the SQL Server Compact 3.5 Objects

After you add the System.Data.SqlServerCe namespace, you can start to code against it by using the SQL Server Compact 3.5 objects. The following code example shows how to use the Engine object to create a new SQL Server Compact 3.5 database file.

using System;
using System.Data.SqlServerCe;
using System.IO;

public class NETCF_SqlCeEngine
{
    public void CreateDB()
    {
        File.Delete("Test.sdf");
        string connString = "Data Source='Test.sdf'; LCID=1033;   Password = <enterStrongPasswordHere>; Encrypt = TRUE;";
        SqlCeEngine engine = new SqlCeEngine(connString);
        engine.CreateDatabase();
    }
}
Imports System
Imports System.Data.SqlServerCe
Imports System.IO

Public Class NETCF_SqlCeEngine

    Public Sub CreateDB()
        File.Delete("Test.sdf")
        Dim connString As String = "Data Source='Test.sdf'; LCID=1033; Password = <enterStrongPasswordHere>; Encrypt = TRUE;"
        Dim engine As New SqlCeEngine(connString)
        engine.CreateDatabase()
    End Sub
End Class 

See Also

Reference

System.Data.SqlServerCe Namespace (.NET Framework Reference Documentation)
System.Data.SqlServerCe

Other Resources

Developer's Guide (SQL Server Compact)
Tutorials (SQL Server Compact 3.5)
System.Data.SqlServerCe Namespace Objects

Help and Information

Getting Assistance (SQL Server Compact 3.5 Service Pack 1)