Cannot Connect to LocalDB from .NET
Join the DZone community and get the full member experience.Join For Free
you are trying something simple – just open a connection to a localdb instance from .net but it is failing with a sqlexception which has the following message:
a network-related or instance-specific error occurred while establishing a connection to sql server. the server was not found or was not accessible. verify that the instance name is correct and that sql server is configured to allow remote connections. (provider: named pipes provider, error: 40 - could not open a connection to sql server)
you know localdb is working, since you copied the connection string from within visual studio, but it just doesn’t work!
the cause of this issue is the connection string, however it may not be obvious at first glance:
var sqlconnection = new sqlconnection("data source=(localdb)\v11.0;initial catalog=astronauts;integrated security=true;connect timeout=5");
the root cause is the data source and the \v in it – normally when you have a \ it complains unless it is a valid escape sequence like \r or \t and since it is not complaining here, it must be a valid escape sequence, just maybe one you are not familiar with, as \v is for a vertical tab .
the solution is easy when you realise that the escape sequence is messing with you, just add another slash:
var sqlconnection = new sqlconnection("data source=(localdb)\\v11.0;initial catalog=astronauts;integrated security=true;connect timeout=5");
Published at DZone with permission of Robert Maclean, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.