Building Applications for Desktops (SQL Server Compact)

You can use SQL Server Compact 3.5 as the data store for computer applications. In this topic, you will learn how to add SQL Server Compact 3.5 to a 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 Computer Project

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

Note

If you are developing a Tablet PC application on a computer that is not 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 computer 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 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 select Add Reference.

    Note

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

  6. (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 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 3.5 assembly. Install SQL Server Compact 3.5 by running the SQL Server Compact 3.5 installer for computers.

To add a reference to SQL Server Compact 3.5

  1. In Solution Explorer, right-click References and select 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 3.5 is installed - %Program Files%\Microsoft SQL Server Compact Edition\v3.5.

  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 select 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 3.5 Objects

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

    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)

Other Resources

System.Data.SqlServerCe Namespace Objects

Help and Information

Getting Assistance (SQL Server Compact 3.5 Service Pack 1)