How do I connect to a desktop SQL Server database from Window CE?

The way to connect to a desktop SQL Server database from a smart device project is different from connecting to a local SQL Server Compact database. It is more similar to a desktop Windows Forms project, as demonstrated in the following steps:

  1. Create a C# smart device project in Visual Studio.

  2. Add a reference to System.Data.SqlClient.

  3. Add the following sample code to somewhere in your project (e.g. Form.Load):

    using System.Data.SqlClient;

    ... ...
    string connString = "Data Source=<server’s address>,<port if necessary>;Initial Catalog=SqlTestDb;User Id=id;Password=pwd;";
    string cmdText = "select Id, Name from Customers";
    using (SqlConnection conn = new SqlConnection(connString))
        SqlCommand cmd = new SqlCommand(cmdText, conn);
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
            System.Diagnostics.Debug.WriteLine(string.Format("{0}, {1}", reader[0], reader[1]));
  1. You may need to install the correct SQL Client CAB on some devices. You can find the CAB in one of the following locations on your computer:
        %Program Files%\Microsoft Visual Studio 9.0\SmartDevices\SDK\SQL Server\Client 
        %Program Files%\Microsoft SQL Server Compact Edition\v3.5\Devices\Client\

Remark: if you deploy the project directly to the device, Visual Studio shall do this step for you.

  1. Make sure the device has network connectivity. Deploy the project to the device and then run it.


(see also: MS sql server compact intallation)

Simone Callegari
L3 Mobile Computer Specialist Support SW Engineer | Datalogic