connectiondesktop-applicationactivesyncsdf

How do I connect to SDF on a Mobile device from desktop application?


C# WinForms .Net 3.5 to SQL CE 3.5 on Mobile 6.1 Device

I'd like to make a connection from a desktop application to a SDF database on my Windows Mobile device while it's connected via ActiveSync. Visual Studio lets me create a Data Connection to my device. The connections tests OK and I can view the data in the database using Visual Studio.

I then create a form and try to fill a DataGridView. When I run the program I get an error that the path to the data base is not valid.

How am I supposed to specify the Mobile device path in the connection string?

In my App.Config, I've tried variations on the path, but none of them work:

connectionString="Data Source=Mobile Device\Program Files\SqlCeViaActiveSync\Orders.sdf"

connectionString="Data Source=\Mobile Device\Program Files\SqlCeViaActiveSync\Orders.sdf"

connectionString="Data Source=Program Files\SqlCeViaActiveSync\Orders.sdf"

connectionString="Data Source=\Program Files\SqlCeViaActiveSync\Orders.sdf"

The full connection string section looks like this:

<connectionStrings>
    <add name="SqlCeViaActiveSync.Properties.Settings.OrdersConnectionString"
        connectionString="Data Source=Mobile Device\Program Files\SqlCeViaActiveSync\Orders.sdf"
        providerName="Microsoft.SqlServerCe.Client.3.5" />
</connectionStrings>

Also, I did make a reference to Microsoft.SqlServerCe.Client, as I found a few articles that mentioned it was necessary.

Can anyone point me to some recent articles/samples or let me know what I'm doing wrong?

Thanks!


Solution

  • I just found that the following works:

    SqlCeConnection conn = new SqlCeConnection(@"Data Source='Mobile Device\Program Files\SqlCeViaActiveSync\Orders.sdf';");
            conn.Open();
            using (SqlCeTransaction trans = conn.BeginTransaction())
            {
                using (SqlCeCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT [OrderNumber] FROM [Orders];";
                    trans.Commit();
                    SqlCeDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        this.listBox1.Items.Add((string)dr["OrderNumber"]);
                    }
    
                    MessageBox.Show(dr.RecordsAffected.ToString());
                }
            }
            conn.Close();
    

    It wasn't exactly what I was looking for but will work for this application.