如何创建的SQL Server Compact 3.5 .sdf文件并连接到它?到它、文件、Server、SQL

2023-09-05 04:37:28 作者:白昼怎懂夜的黑

我创建了一个SQL Server Compact数据库(。自卫队文件),我想连接到它做一些插入,删除...

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

这是我的创作code吧:

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

是真的吗?

我怎样才能连接到它?

How can I connect to it?

推荐答案

这是连接字符串被打破了。

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.

请仔细阅读一个例子的比如这个,这是为连接SQL紧凑例如C#第一个谷歌的结果:

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