Jimmy D
Jimmy D

Reputation: 5376

SQL Server Compact Edition and VB/C#

Can anyone provide a clear and simple example of how I can open a SSCE database and query it with a SELECT statement? Ultimately I'll need to do this on a system that I have no control over so whatever method/provider I use must be available by default on a standard Windows machine.

Thanks!

Upvotes: 0

Views: 839

Answers (3)

Jimmy D
Jimmy D

Reputation: 5376

For anyone struggling with this, look at this article:

http://msdn.microsoft.com/en-us/library/aa983326.aspx

Then it's as simple as this:

Imports System.Data.SqlServerCe

Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Try

        Using conn As New SqlCeConnection
            conn.ConnectionString = "Data Source=c:\Reporting.Database.sdf;Persist Security Info=False;"
            conn.Open()
            MsgBox("opened")
            conn.Close()
        End Using

    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub
End Class

Upvotes: 0

czuroski
czuroski

Reputation: 4332

The following should work. you will have to add a reference to System.Data.SqlServerCe and create a using statement for it.

    string connectionString = "my connection string";
    string queryString = "select column from mytable where mycolumn = 'somevalue'";    
    using (var cnn = new SqlCeConnection(connectionString))
                        {
                            using (var cmd = new SqlCeCommand(queryString, cnn))
                            {
                                cnn.Open();

                                var da = new SqlCeDataAdapter(cmd);
                                var ds = new DataSet();
                                da.Fill(ds);

                            }
                        }

You can also use cmd.Parameters.Add after you call cnn.Open(); to add parameters to your query.

Upvotes: 0

Related Questions