【发布时间】:2017-04-10 23:47:29
【问题描述】:
我有一个 C# 应用程序,我正在尝试使用 LocalDB。截至目前,当我启动我的应用程序时,我遇到了这个错误。
Cannot create named instance.
The parameter for the LocalDB Instance API method is incorrect. Consult the
API documentation.
我的 App.config 看起来像这样。
<system.data.localdb>
<!-- I saw this on another website, I'm not sure if it's neeed or not.-->
<localdbinstances>
<add name="MSSQLLocalDB" version="mssqllocaldb" />
</localdbinstances>
</system.data.localdb>
<connectionStrings>
<add name="DiaProdConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;Integrated Security=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory
type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
当我运行 Sqllocaldb.exe 版本时,我得到以下输出。
..> SqlLocalDB.exe versions
Microsoft SQL Server 2012 (11.0.3000.0)
Microsoft SQL Server 2014 (12.0.4459.0)
Microsoft SQL Server 2016 (13.0.1601.5)
理想情况下,应用程序会启动,当它发现数据库不存在时,它会创建它。
以下行在开发中确实有效,但如果在具有 2014 版 SQLLocalDB 的测试系统上失败,这就是我决定使用新字符串的原因。此外,人们说旧字符串已被弃用,仅适用于 2012 年。
<add name="DiaProdConnection" connectionString="data source=(localdb)\v11.0;initial catalog=DiaDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework;Connection Timeout=15;Connection Lifetime=0;Min Pool Size=10;Max Pool Size=20;Pooling=true;" providerName="System.Data.SqlClient" />
非常感谢任何帮助。
【问题讨论】:
-
适用于 2012 数据库的代码有
connectionString="data source=(localdb)\ **v11.0**。您可以从包含的内容中看到 SQL Server 2012 是版本 11。SQL Server 2014 是版本 12。另外,我会将<add name="MSSQLLocalDB" version="mssqllocaldb" />更改为<add name="MSSQLLocalDB" version="12.0" />
标签: c# .net sql-server localdb