如何创建的SQL Server Compact 3.5 .sdf文件并连接到它? [英] How I create a SQL Server Compact 3.5 .sdf file and connect to it?

查看:450
本文介绍了如何创建的SQL Server Compact 3.5 .sdf文件并连接到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个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#":

这篇关于如何创建的SQL Server Compact 3.5 .sdf文件并连接到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆