Building Application for Desktop and Tablet PCs (SQL Server Compact Edition)

You can use Microsoft SQL Server 2005 Compact Edition (SQL Server Compact Edition) as the data store for desktop and Tablet PC applications. In this topic, you will learn how to add SQL Server Compact Edition to your Microsoft Visual Studio product, and then code against the System.Data.SqlServerCe namespace. Managed reference topics for the System.Data.SqlServerCe namespace are available in the .NET Framework Reference Documentation.

Creating a Desktop or Tablet PC Project

To create a new desktop or Tablet PC application, you first create a Windows project in Visual Studio 2005. For a Tablet PC application, you must then add a reference to the Microsoft.Ink namespace.

Note

If you are not developing a Tablet PC application on a computer that is running Windows XP Tablet PC Edition, you must first install the Microsoft Windows XP Tablet PC Edition Development Kit. You can download the latest version from the Mobile and Embedded Application Developer Center.

To create a new desktop or Tablet PC 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 Windows.

  3. In the Templates list, select Windows 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).

  5. (Optional) 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.

  1. (Optional) In the list of .NET assemblies, select Microsoft Tablet PC API, and then click OK.
    The list of references now includes Microsoft.Ink.

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. Install SQL Server Compact Edition by running the SQL Server Compact Edition installer for desktop computers and Tablet PC computers (SQLServerEv31-EN.msi). For more information, see Deploying Desktop and Tablet PC Applications.

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 Add References dialog box, select Browse.

  3. Navigate to the folder where SQL Server Compact Edition is installed - %Program Files%\Microsoft SQL Server Compact Edition\v3.1.

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

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

  6. At the top of the code for the form, add a directive to use the System.Data.SqlServerCe namespace. For a Tablet PC application, also add a directive to use the Tablet PC API:

    • C#

      using System.Data.SqlServerCe;
      using Microsoft.Ink;
      
    • Visual Basic

      Imports System.Data.SqlServerCe
      Imports Microsoft.Ink
      

Using the SQL Server Compact Edition Objects

After the System.Data.SqlServerCe namespace has been added, 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 MySqlCeEngine
{
    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 MySqlCeEngine

    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)

Help and Information

Getting SQL Server Compact Edition Assistance