Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .Net SqlConnection to LocalDB

I feel silly for even having to ask this, but I must be missing something.

I am simply trying to open a new SqlConnection to my LocalDB instance on my development machine. Visual Studio 2013.

I can and am connected to LocalDB from SSMS and am not having any issues. In the solution I am working on, I can and have succesfully connected to LocalDB with Entity Framework.

In another project within the same solution I am attempting to connect to LocalDB with an old school SqlConnection object, and this is where I am just totally failing.

Here is the connection string I am trying to use,

SqlConnection SqlConn = new SqlConnection("Server=(LocalDb)\v11.0;Initial Catalog=DBNAME;Integrated Security=SSPI;Trusted_Connection=yes;");

When I call SqlConn.Open() I get an error message saying it can't find or connect to the database.

This doesn't really matter as the connection string will be changed when this gets merged to production, but I can't for the life of me figure out why I am unable to get this SqlConnection object to talk with LocalDB

like image 729
mituw16 Avatar asked Dec 08 '14 20:12

mituw16


1 Answers

It looks like you might need to escape the slash in your connection string.

Turn this: "Server=(LocalDb)\v11.0;Initial Catalog=DBNAME;Integrated Security=SSPI;Trusted_Connection=yes;"

Into this: "Server=(LocalDb)\\v11.0;Initial Catalog=DBNAME;Integrated Security=SSPI;Trusted_Connection=yes;"

like image 193
Zind Avatar answered Oct 08 '22 14:10

Zind