SqlServerCe 的 EntityConnection 字符串

发布于 2024-11-09 07:42:41 字数 2912 浏览 0 评论 0原文

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

我尝试按照如何:构建EntityConnection连接字符串上的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.

我可能做错了什么?

我发现此相关< a href="http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection- to-sql-server-microsoft-sql-server-error/" rel="nofollow">SQL Server 修复,但修复需要是我可以在代码中设置的内容,以便我的项目能够在我们的客户的计算机位于其他地点,没有网络管理员。

How would I create an EntityConnection string for an SqlServerCe database?

I tried following the MSDN Example on How to: Build an EntityConnection Connection String

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

However, but it throws an error:

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)

With some searching on here, I was able to find info stating that the MSDN Example returns an SqlClient connection.

However, I was not able to find out how to return an Sql CE Connection.

I tried supplying the EntityConnection the SQL CE Connection String that allows me to open the .SWF database and read it conventionally:

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

This version throws a different error:

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

What could I be doing wrong?

I found this related SQL Server Fix, but the fix needs to be something I can set in my code so that my project will run on our Customers' computers at other locations without network administrators.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2024-11-16 07:42:41

您需要在 app.config 中注册 CE 提供程序。另外,据我所知,“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>

You need to register the CE provider in your app.config. Also, AFAIK, "Microsoft.SqlServerCe.Client.3.5" is the pre-release name of the provider; the RTM is "System.Data.SqlServerCe.3.5".

Try changing PROVIDER to match the second string, and add this to 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文