简体   繁体   中英

Create SQL Server CE database file programmatically

如何以编程方式创建新的SQL Server Compact数据库文件(.sdf),而无需从中复制现有模板文件?

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();

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM