title: Create SQLCE database programmatically
body: <p><strong>[EDITED - with answer]</strong></p>
  
  <p>Following is my code to create SQL CE database programmatically:</p>
  
  <pre><code>/* get the Path */
  var directoryName = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
  var fileName = System.IO.Path.Combine(directoryName, "Foo2Database.sdf");
  
  /* check if exists */
  if (File.Exists(fileName))
      File.Delete(fileName);
  
  string connStr = @"Data Source = " + fileName;
  
  /* create Database */
  SqlCeEngine engine = new SqlCeEngine(connStr);
  engine.CreateDatabase();
  
  /* create table and columns */
  using (SqlCeConnection conn = new SqlCeConnection(connStr))
  {
      using (SqlCeCommand cmd = new SqlCeCommand(@"CREATE TABLE FooTable (Foo_ID int, FooData NVARCHAR(200))", conn))
      {
          try
          {
              conn.Open();
              cmd.ExecuteNonQuery();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.ToString());
          }
          finally
          {
              conn.Close();
          }
  
      }
  }
  </code></pre>