SqlServerCe 的 EntityConnection 字符串
如何为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 app.config 中注册 CE 提供程序。另外,据我所知,“Microsoft.SqlServerCe.Client.3.5”是提供程序的预发布名称; RTM 是“System.Data.SqlServerCe.3.5”。
尝试更改 PROVIDER 以匹配第二个字符串,并将其添加到 app.config 中:
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: