How I create a SQL Server Compact 3.5 .sdf file and connect to it?

此生再无相见时 提交于 2019-12-11 03:12:10

问题


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

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

Is it true?

How can I connect to it?


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



来源:https://stackoverflow.com/questions/10966559/how-i-create-a-sql-server-compact-3-5-sdf-file-and-connect-to-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!