mit*_*w16 5 .net c# sqlcommand sqlconnection
我甚至不得不问这个问题,我感到愚蠢,但我必须遗漏一些东西.
我只是尝试在我的开发机器上打开一个新的SqlConnection到我的LocalDB实例.Visual Studio 2013.
我可以并且从SSMS连接到LocalDB并且没有任何问题.在我正在开发的解决方案中,我可以成功地使用Entity Framework连接到LocalDB.
在同一解决方案中的另一个项目中,我尝试使用旧的学校SqlConnection对象连接到LocalDB
,这就是我完全失败的地方.
这是我想要使用的连接字符串,
SqlConnection SqlConn = new SqlConnection("Server=(LocalDb)\v11.0;Initial Catalog=DBNAME;Integrated Security=SSPI;Trusted_Connection=yes;");
Run Code Online (Sandbox Code Playgroud)
当我打电话时,SqlConn.Open()我收到一条错误消息,说它无法找到或连接到数据库.
这并不重要,因为当它被合并到生产时连接字符串将被更改,但我不能为我的生活找出为什么我无法让这个SqlConnection对象与LocalDB交谈
看起来您可能需要在连接字符串中转义斜杠.
转过来:
"Server=(LocalDb)\v11.0;Initial Catalog=DBNAME;Integrated Security=SSPI;Trusted_Connection=yes;"
进入:
"Server=(LocalDb)\\v11.0;Initial Catalog=DBNAME;Integrated Security=SSPI;Trusted_Connection=yes;"