SqlServerCe的EntityConnection字符串

时间:2011-05-24 16:31:57

标签: c# entity-framework connection-string

如何为 SqlServerCe 数据库创建 EntityConnection 字符串?

我尝试在How to: Build an EntityConnection Connection String

上关注MSDN示例
const string PROVIDER = "System.Data.SqlClient";

static string BuildEntityConnString(string dbFileName, string password) {
  // dbFileName is filename without the extension
  SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
  sqlBuilder.DataSource = PROVIDER;
  sqlBuilder.InitialCatalog = dbFileName;
  sqlBuilder.IntegratedSecurity = true;
  sqlBuilder.Password = password;
  string providerString = sqlBuilder.ToString();
  EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
  entityBuilder.Provider = PROVIDER;
  entityBuilder.ProviderConnectionString = providerString 
  entityBuilder.Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", dbFileName);
  string connString = entityBuilder.ToString();
  using (EntityConnection con = new EntityConnection(connString)) {
    con.Open();
    Console.WriteLine("{0} Entity String created.", dbFileName);
    con.Close();
  }
  return connString;
}

但是,它会引发错误:

A network-related or instance-specific error occurred while establishing a 
connection to SQL Server. The server was not found or was not accessible. Verify 
that the instance name is correct and that SQL Server is configured to allow 
remote connections. (provider: Named Pipes Provider, error: 40 - Could not open 
a connection to SQL Server)

通过搜索,我能够找到信息,说明MSDN示例返回 SqlClient 连接。

但是,我无法找到如何返回Sql CE连接。

我尝试向EntityConnection提供SQL CE连接字符串,该字符串允许我打开.SWF数据库并按常规读取它:

const string PROVIDER = "Microsoft.SqlServerCe.Client.3.5";

static string BuildEntityConnString(string dbFileName, string password) {
  EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
  entityBuilder.Provider = PROVIDER;
  entityBuilder.ProviderConnectionString = myCeConnectionString;
  entityBuilder.Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", dbFileName);
  string connString = entityBuilder.ToString();
  using (EntityConnection con = new EntityConnection(connString)) {
    con.Open();
    Console.WriteLine("{0} Entity String created.", dbFileName);
    con.Close();
  }
  return connString;
}

此版本引发了不同的错误:

Unable to find the requested .Net Framework Data Provider.
It may not be installed.

我可能做错了什么?

我发现这与SQL Server Fix相关,但修复需要我可以在我的代码中设置,以便我的项目可以在没有网络管理员的其他位置的客户计算机上运行。

1 个答案:

答案 0 :(得分:1)

您需要在app.config中注册CE提供程序。此外,AFAIK,“Microsoft.SqlServerCe.Client.3.5”是提供者的预发布名称; RTM是“System.Data.SqlServerCe.3.5”。

尝试更改PROVIDER以匹配第二个字符串,并将其添加到app.config:

  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.3.5" />
      <add
        name="Microsoft SQL Server Compact Data Provider"
        invariant="System.Data.SqlServerCe.3.5"
        description=".NET Framework Data Provider for Microsoft SQL Server Compact"
        type="System.Data.SqlServerCe.SqlCeProviderFactory,
          System.Data.SqlServerCe,
          Version=3.5.0.0,
          Culture=neutral,
          PublicKeyToken=89845dcd8080cc91"
      />
    </DbProviderFactories>
  </system.data>