Building Smart Device Applications (SQL Server Compact Edition)

In this section, you will learn how to create a new smart device project in Microsoft Visual Studio 2005, add Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) functionality to the project, and then create a new SQL Server Compact Edition 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 Microsoft Visual Studio 2005. 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 Edition databases, you use the System.Data.SqlServerCe namespace. This namespace enables you to do the following in a SQL Server Compact Edition database:

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

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

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

Creating a Smart Device Project

To create a new smart device application, you must first create the project in Visual Studio 2005.

To create a new smart device project

  1. In Visual Studio 2005, 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 Edition to the Project

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

To add a reference to SQL Server Compact Edition

  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:

    • C#

      using System.Data.SqlServerCe;
      
    • Visual Basic

      Imports System.Data.SqlServerCe
      

Using the SQL Server Compact Edition Objects

After you add the System.Data.SqlServerCe namespace, you can start to code against it by using the SQL Server Compact Edition objects. The following code example shows how to use the Engine object to create a new SQL Server Compact Edition 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=\"s$;2'!dS64\"; 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=""s$;2'!dS64""; Encrypt = TRUE;"
        Dim engine As New SqlCeEngine(connString)
        engine.CreateDatabase()
    End Sub
End Class 

See Also

Reference

Programming Reference (SQL Server Compact Edition)
System.Data.SqlServerCe Namespace Overview
System.Data.SqlServerCe Namespace (.NET Framework Reference Documentation)

Other Resources

Tutorials (SQL Server Compact Edition)

Help and Information

Getting SQL Server Compact Edition Assistance