Ali Adlavaran
Ali Adlavaran

Reputation: 3735

How I create a SQL Server Compact 3.5 .sdf file and connect to it?

I have created a SQL Server Compact Database (.sdf file) and I want to be connected to it for do some insert , delete ... .

This is my creation code for it:

  if (File.Exists(dbfilename))
     File.Delete(dbfilename);

  string connectionString = "Data Source=" + dbfilename + """;

  SqlCeEngine engine = new SqlCeEngine(connectionString);
  engine.CreateDatabase();
  engine.Dispose();

  SqlCeConnection conn = null;

  try
  {
        conn = new SqlCeConnection(connectionString);
        conn.Open();

        SqlCeCommand cmd = conn.CreateCommand();
        cmd.CommandText = "CREATE TABLE Contacts (ID uniqueidentifire, Address ntext)";
        cmd.ExecuteNonQuery();
  }
  catch { }
  finally
  {
         conn.Close();
  }

Is it true?

How can I connect to it?

Upvotes: 2

Views: 9415

Answers (1)

dodexahedron
dodexahedron

Reputation: 4657

That connection string is broken.

"

is not a valid entity where you are trying to use it. Fix your connection string.

Also, you will need to associate the command with the connection, either in the constructor or after the fact.

Please read through an example such as this one, which was the first google result for "connect sql compact example c#":

Upvotes: 1

Related Questions