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

Visual Studio 2008 is the preferred environment for developing applications for smart devices with SQL Server Compact 3.5 SP2. Visual Studio 2010 does not have support for developing smart device applications. For more information, see Installing a Development Environment.

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.

For Visual Basic, first expand Other Languages.

  1. In the Templates list, select Smart Device Project.

  2. Provide a name and location for your project, and then click OK.The Add New Smart Project dialog box opens.

  3. In the Templates list, select Device Application.

  4. From the Target platform drop-down list, select the platform for which you want to create the project. For example, if you are developing a Windows Mobile 5.0 Pocket PC application, select Windows Mobile 5.0 Pocket PC SDK.

  5. From the .NET Compact Framework version drop-down list, select the version of the .NET Compact Framework to use for your application.

  6. Click OK.

    Visual Studio creates a new project and displays the main form (Form1). The display will look similar to a device for the target platform 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.

    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. Navigate to the following directory:

      C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices

    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
    

Configuring SQL Server Compact for Private Deployment

To build an application that uses private deployment for SQL Server Compact 3.5 you must ensure that the .NET Compact Framework data provider and any necessary native DLLs are deployed in the application's output directory. If you want your application to use private deployment for SQL Server Compact 3.5, you must follow the steps in this section. If your application does not use private deployment, skip this section. For more information about private deployment of SQL Server Compact 3.5, see Private Deployment vs. Central Deployment (SQL Server Compact).

To configure the application for private deployment of SQL Server Compact

  1. Configure the .NET Compact Framework data provider for SQL Server Compact 3.5 assembly to be copied to the output directory when the project is built.

    1. In Solution Explorer, expand References, right-click System.Data.SqlServerCe, and select Properties.

    2. In System.Data.SqlServerCeReference Properties, set Copy Local to True.

  2. Add the necessary SQL Server Compact 3.5 native libraries to the project.

    1. In Solution Explorer, right-click the project, point to Add, and select Existing Item.

    2. In the Add Existing Item dialog box, browse to the location in %Program Files%\Microsoft SQL Server Compact Edition\v3.5\Devices\ where the assemblies for your device platform are located. For example: C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices\wce500\armv4i.

    3. Control-click on each of the necessary native libraries for your project to select them in the file list, and then click OK.

      At a minimum you must add the following libraries: sqlceme35.dll, sqlceqp35.dll, and sqlcese35.dll.

  3. Configure each of the native libraries added in the previous step to be copied to the output directory when the project is built.

    1. In Solution Explorer, right-click the library (for example sqlcese35.dll), and select properties.

      If the assemblies are not listed in Solution Explorer, click Show All Files at the top of Solution Explorer.

    2. In file Properties, select Copy Always or Copy if Newer from the Copy to Output Directory drop-down list.

F5 deployment of a smart device application from Visual Studio performs a central install of SQL Server Compact 3.5 on the smart device. For this reason, if you deploy your application from within Visual Studio, the application uses the version of SQL Server Compact 3.5 that is installed by Visual Studio on your device during deployment; even if the application is built for private deployment of SQL Server Compact 3.5.

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 

Reference

System.Data.SqlServerCe

Deploying Smart Device Applications

Other Resources

Developer's Guide (SQL Server Compact)