2

我创建了一个 SQL Server Compact 数据库(.sdf文件),我想连接到它以执行一些插入、删除...。

这是我的创建代码:

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

这是真的吗?

我怎样才能连接到它?

4

1 回答 1

1

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#":

于 2012-06-10T05:35:20.920 回答