C# .Net SqlConnection to LocalDB
Asked Answered
S

3

5

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

Seidule answered 8/12, 2014 at 20:24 Comment(4)
Are you certain you have an instance named v11.0?Nexus
Maybe? I almost never use LocalDB...Here is the connection string that I have set for Entity Framework to talk to LocalDB <add name="NHFolding" connectionString="Server=(LocalDb)\v11.0;Integrated Security=true;Initial Catalog=DBNAME" providerName="System.Data.SqlClient" />Seidule
Is the project a web project or something that would be running as a different user?Nexus
Well now I really do feel like an idiot....I forgot to escape the backslash in the connection string in the C# Code....It's fixed now. Thank you @DStanleySeidule
E
9

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

Emolument answered 8/12, 2014 at 20:32 Comment(3)
This was it. Thank you, ZindSeidule
@Seidule Best way to show thank you is to mark answer as correct :). Good find Zind!Balder
I plan on it, @Balder .. Have to wait 2 more minutes until it will let me accept the answer :)Seidule
C
0

I found this nuget package that wraps up working with SQLLocalDB.

ISqlLocalDbProvider provider = new SqlLocalDbProvider();
ISqlLocalDbInstance instance = provider.GetOrCreateInstance("MyInstance");

instance.Start();

using (SqlConnection connection = instance.CreateConnection())
{
    connection.Open();

    // Use the connection...
}

instance.Stop();
Censorious answered 4/6, 2018 at 10:6 Comment(0)
M
0

In my case I was using app.config in a separate PCL which obviously wasn't being read when ConfigurationManager.ConnectionStrings is looking for ConnectionStrings entries in app.config file.

What solved this was editing (in my case creating as there wasn't any file) the main project's app.config file and it worked great.

Mada answered 11/3, 2019 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.