How to connect to LocalDb
Asked Answered
H

13

126

I installed LocalDb using the SqlLocalDb.msi package and I can connect to it using SSMS using the server name (LocalDb)\v11.0. So far so good. The problem is that when I try to connect to it via a .NET 4.5 application I get the error

The server was not found or was not accessible.

I started with the connection string Data Source=(LocalDb)\v11.0 and then added many things to no avail (integrated security true/sspi, attaching a db, using instances, etc.)

Hobbyhorse answered 21/9, 2012 at 16:29 Comment(2)
Possible duplicate of Unable to connect to localDB in VS2012 – "A network-related or instance-specific error occurred while establishing a connection to SQL Server..."Devault
Looking at all these answers, I thought LocalDb was supposed to make things simpler? Seems to be another layer of complexity and confusion.Pimpernel
L
44

I think you hit the same issue as discussed in this post. You forgot to escape your \ character.

Lilylilyan answered 21/9, 2012 at 17:2 Comment(1)
It only hurts the first time though :-) After that you know immediately what's happening.Lilylilyan
D
137

I am totally unable to connect to localdb with any tool including MSSMA, sqlcmd, etc. You would think Microsoft would document this, but I find nothing on MSDN. I have v12 and tried (localdb)\v12.0 and that didn't work. Issuing the command sqllocaldb i MSSQLLocalDB shows that the local instance is running, but there is no way to connect to it.

c:\> sqllocaldb i MSSQLLocalDB
Name:               MSSQLLocalDB
Version:            12.0.2000.8
Shared name:
Owner:              CWOLF-PC\cwolf
Auto-create:        Yes
State:              Running
Last start time:    6/12/2014 8:34:11 AM
Instance pipe name: np:\\.\pipe\LOCALDB#C86052DD\tsql\query
c:\>
c:\> sqlcmd -L

Servers:
    ;UID:Login ID=?;PWD:Password=?;Trusted_Connection:Use Integrated Security=?;
*APP:AppName=?;*WSID:WorkStation ID=?;

I finally figured it out!! the connect string is (localdb)\MSSQLLocalDB, e.g.:

$ sqlcmd -S \(localdb\)\\MSSQLLocalDB
1> select 'hello!'
2> go

------
hello!

(1 rows affected)    
Doctor answered 12/6, 2014 at 13:15 Comment(8)
I have been sitting here for 2 hours trying to figure out where this db is. Thank youUmbilical
Thank you! It took me forever to figure out it was (localdb)\MSSQLLocalDBAblate
Thank you! I've been looking for (localdb)\MSSQLLocalDB for ages, too.Legislative
thank you. I tried to copy from appsettings file in .net core project: "DefaultConnection": "Server=(localdb)\\mssqllocaldb; It was the double back slashes that got me!Armand
after adding "" around (localdb)\MSSQLLocalDB, the command sqlcmd -S "(localdb)\MSSQLLocalDB" worked for meNavicert
I don't think 1 upvote is enough. For too long I have struggled to figure this out.Hah
@Navicert your edit worked for me tooWitless
The good old escaped "\" that fools everyone :-)Osteotome
H
134

Use (localdb)\MSSQLLocalDB. That is the LocalDB instance intended for applications, independent of Visual Studio version.


Disregard my original answer: "With SQL Server 2014 Express LocalDB, use (localdb)\ProjectsV12. This works in both Visual Studio 2013 and SQL Server 2014 Management Studio." While ProjectsV12 will indeed give you a LocalDB instance, it's the wrong one, intended for use by SQL Server Data Tools.

Haven answered 14/5, 2014 at 15:22 Comment(3)
Nice... I didn't know this and was still using (LocalDb)\v11.0 (SQL Server 11.0.3128) while the server version bumped with (LocalDb)\ProjectsV12 (SQL Server 12.0.2000).Bloodmobile
Thank you!! using (localdb)\MSSQLLocalDB worked for me in mssms 2017Wrap
What happens when you want to deploy to Docker container? How does that get deployed?Interurban
M
48

Use (localdb)\MSSQLLocalDBwith Windows Auth

Mezzorelievo answered 12/3, 2019 at 21:20 Comment(0)
L
44

I think you hit the same issue as discussed in this post. You forgot to escape your \ character.

Lilylilyan answered 21/9, 2012 at 17:2 Comment(1)
It only hurts the first time though :-) After that you know immediately what's happening.Lilylilyan
W
19

To connect to LocalDB from Microsoft SQL Server Management Studio or VS2019 and higher versions use the server name as (LocalDB)\MSSQLLocalDB with Windows Authentication

Whodunit answered 17/1, 2022 at 19:7 Comment(0)
C
2

I was able to connect from SSMS using "(LocalDb)\Projects". That's the way it appears in VS2012 as well.

Confound answered 6/3, 2014 at 14:37 Comment(2)
I found this, too. I had to list the instances using sqllocaldb.exe -i - you'll see there's 3 for 2014 - then start the instance, then my connection string from various online sources was wrong for SSMS/VS14 tools, should be just (localdb)\ProjectsV12Heaves
there's no dash - it's just sqllocaldb.exe iMeloniemelony
A
2

Your Connection string should be like`

Data Source=(localdb)\ProjectsV13;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
Atalanta answered 10/10, 2018 at 17:45 Comment(4)
What is "ProjectsV13"?Pimpernel
ask it vs developers from msViolist
it doesn work from MSSqlServerChekhov
ProjectsV13 is name of database (in the example).Mutule
T
2
 <add name="Default" connectionString="Data Source=(LocalDb)\MSSqlLocalDB; Initial Catalog=CRM_Default_v1; Integrated Security=True"
      providerName="System.Data.SqlClient"/>

your web.config files in visual studio under connectiionString or Go to View > SQL Server Object Viewer > Add Sql Server> add your server there

Triplett answered 3/8, 2019 at 15:0 Comment(0)
M
2

Dont waste time with the stress just use (localdb)\mssqllocaldb as your connection string and also as your as your server name in Microsoft SQL Server, It worked for me and it could work for you if you go through the right process

Ministration answered 24/5, 2022 at 5:21 Comment(0)
D
1

You can connect with MSSMS to LocalDB. Type only in SERVER NAME: (localdb)\v11.0 and leave it by Windows Authentication and it connects to your LocalDB server and shows you the databases in it.

Diner answered 19/6, 2016 at 21:32 Comment(0)
C
1

Suppose: SqlConnection connectionObj = new SqlConnection()

for : connectionObj.ConnectionString -> use server name : (localdb)\\MSSQLLocalDB.

Note: Double back slash

for : App.config -> use server name : (localdb)\MSSQLLocalDB

Note: Single back slash

Cuthbertson answered 22/4, 2020 at 17:6 Comment(1)
Single slash on first line was a typing mistake. Actually, I meant to answer the question. What I have mentioned refers to the pattern in which server names have to be provided in each of the two scenarios,i.e while giving it as [connectionObj.ConnectionString = "Server = (localdb)\\MSSQLLocalDB; Database = database_name; Integrated security = SSPI"] and in config file : [ <add name="ref_name" connectionString="Server=(localDB)\MSSQLLocalDB; Database=database_name; Integrated Security=SSPI" providerName="provider_name" />].Cuthbertson
T
0

To locate a DB from SQL Server management studio can be done through browse - in the connect to database screen

enter image description here

Also make sure a local database is installed during installation:

enter image description here

Tricostate answered 4/8, 2020 at 4:30 Comment(0)
A
0

If you are running the MSSQL Database from a docker image (as described on the link here), in the Server Name field, just type 127.0.0.1, <port number>.

Reference: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-manage-ssms?view=sql-server-ver16

Aube answered 17/4, 2023 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.