Create SQL Server CE database file programmatically

C#.NetSql ServerDatabaseSql Server-Ce

C# Problem Overview


How can I create a new SQL Server Compact database file (.sdf) programmatically, without having an existing template file to copy from?

C# Solutions


Solution 1 - C#

There is some good info here: Create a SQL Server Compact Edition Database with C#

string connectionString = "DataSource=\"test.sdf\"; Password=\"mypassword\"";
SqlCeEngine en = new SqlCeEngine(connectionString);
en.CreateDatabase();

Solution 2 - C#

First, go click on the Browse tab. Now navigate to C:\Program Files\Microsoft SQL Server Compact Edition\v3.1. Now pick the System.Dadta.SqlServerCe.dll, click on it, then click on OK to pull in the reference.

Now let’s write some code. First, go to the head of the class, whoops I mean header of your class and let’s create a using reference.

using System.Data.SqlServerCe;
using System.IO;

  string connectionString;
  string fileName = "aminescm.sdf";
  string password = “aminescm”;

  if (File.Exists(fileName))
  {
    File.Delete(fileName);
  }

  connectionString = string.Format(
    "DataSource=\"{0}\"; Password='{1}'", fileName, password);
  SqlCeEngine en = new SqlCeEngine(connectionString);
  en.CreateDatabase();

You can find more and more about this, it's really an amazing web site:

http://arcanecode.com/arcane-lessons/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew ArnottView Question on Stackoverflow
Solution 1 - C#D'Arcy RittichView Answer on Stackoverflow
Solution 2 - C#aminescmView Answer on Stackoverflow