C# 在 VS2012 中无法连接到 localDB – “在与 SQL Server 建立连接时发生与网络相关或特定于实例的错误...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12463228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:21:17  来源:igfitidea点击:

Unable to connect to localDB in VS2012 – "A network-related or instance-specific error occurred while establishing a connection to SQL Server..."

c#sql-serverado.net.net-4.5localdb

提问by Varun K

This is strange as I'm able to connect to localDB through SSMS 2008R2 with the same connection string ("Data Source=(LocalDB)\v11.0;Integrated Security=true")

这很奇怪,因为我可以使用相同的连接字符串(“ Data Source=(LocalDB)\v11.0;Integrated Security=true”)通过 SSMS 2008R2 连接到 localDB

Only C#code is unable to connect, I have tried increasing login time with Connect Timeout=60but no luck.

只有C#代码无法连接,我尝试增加登录时间,Connect Timeout=60但没有成功。

I have also tried specifying the database Initial Catalog=&lt;databasename&gt;where the <databasename> is the one I have created on localdb via ssms.

我还尝试指定数据库Initial Catalog=&lt;databasename&gt;,其中 <databasename> 是我通过 ssms 在 localdb 上创建的数据库。

Any pointers as to why is this not connecting?

关于为什么这不连接的任何指示?

采纳答案by Krzysztof Kozielczyk

Any chance it is because you forgot to double-escape the backslash? Did you try this:

有没有可能是因为您忘记对反斜杠进行双重转义?你试过这个吗:

"Data Source=(LocalDB)\\v11.0;Integrated Security=true"

"Data Source=(LocalDB)\\v11.0;Integrated Security=true"

Or even better, you could just use the @mark to disable character escaping for the entire connection string:

或者更好的是,您可以使用@标记来禁用整个连接字符串的字符转义:

@"Data Source=(LocalDB)\v11.0;Integrated Security=true"

@"Data Source=(LocalDB)\v11.0;Integrated Security=true"