无法连接到VS2012中的localDB—“在建立连接到SQL服务器时发生了与网络相关的或实例化的错误…”

时间:2020-12-04 15:42:01

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")

这很奇怪,因为我可以使用相同的连接字符串通过SSMS 2008R2连接到localDB(“数据源=(localDB)\v11.0;集成安全性=true”)

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

只有c#代码无法连接,我尝试增加连接超时=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.

我还尝试指定数据库初始目录=<databasename>其中 是我通过ssms在localdb上创建的。

Any pointers as to why is this not connecting?

关于为什么它不连接有什么建议吗?

1 个解决方案

#1


34  

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

有没有可能是因为你忘了把反斜杠转义?你试试这个:

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

“数据源=(LocalDB)\ \ v11.0;综合安全= 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"

@“数据源=(LocalDB)\ v11.0;综合安全= true”

#1


34  

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

有没有可能是因为你忘了把反斜杠转义?你试试这个:

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

“数据源=(LocalDB)\ \ v11.0;综合安全= 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"

@“数据源=(LocalDB)\ v11.0;综合安全= true”