56

I am getting an error, While I am trying to connect (LocalDB)\MSSQLLocalDB through SQL Server management studio. I also tried to login with default database as master the error is same.

enter image description here Here is the Server details. enter image description here

vela
  • 147
  • 10
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
  • 2
    Also remember to not escape one backslash like `appsettings.json` `ConnectionStrings` `(localdb)\\mssqllocaldb`. Correct is `(localdb)\mssqllocaldb`. – Ogglas May 17 '21 at 09:53

2 Answers2

131

Warning: this will delete all your databases located in MSSQLLocalDB. Proceed with caution.

The following command through sqllocaldb utility works for me.

sqllocaldb stop mssqllocaldb
sqllocaldb delete mssqllocaldb
sqllocaldb start "MSSQLLocalDB"

enter image description here

After that I restarted the sql server management studio, and it is successfully established connection through (LocalDB)\MSSQLLocalDB

Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
  • 4
    Restarting VisualStudio is also required if this is the DB tool you're using (as you mentionned for SSMS). – Askolein Feb 17 '17 at 11:54
  • @MusakkhirSayyed worked for me too ! Thanks! .. Strange error . – vibs2006 Jul 28 '17 at 07:12
  • 27
    Warning ! this will delete all your databases located in MSSQLLocalDB if you use the delete command – Malick Aug 07 '17 at 06:46
  • 1
    Trying what you posted above only worked for me after I tricked the MSSQLLocalDb instance into erroring out. This was because I was getting an error on Windows API call LogSqlDiagRec when trying to run sqllocaldb stop mssqllocaldb. I updated the HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\UserInstances\ParentInstance value from MSSQL13E.LOCALDB to MSSQL13.1E.LOCALDB and back to MSSQL13E.LOCALDB again. This invalid value 13.1 forced MSSQLLocalDB into a Stopped State. This allowed me to delete and start the mssqllocaldb again. Once I did that I was able to login to localdb in SSMS. – Guzumba Dec 11 '18 at 04:17
  • I had a problem like this, but where the "Instance pipe name" was empty, I tried googling the problem but ended up trying this solution, and it also worked for me. – Droa Aug 08 '19 at 06:47
  • I'm just dealing with a Visual Studio issue where my unit tests throw these `login failed for user` errors (I don't even have SQL Enterprise Studio installed). Even though I drop the connections from my `Server Explorer` - nothing works until I restart Visual Studio – bkwdesign Dec 05 '19 at 13:52
  • You Sir, are a genius!! I was struggling my butt off trying to figure out why a winforms app did not work on a brand new pc of a client when he resinstalled, but it works on every other pc we install it on. After trying everything, this was the only solution that worked! – Christopher Smit Jul 02 '20 at 08:55
  • I'm getting an error when starting the instance (third command): Cannot create an automatic instance. How do I get past this please? – JustAPup Dec 02 '21 at 20:36
  • Thank you. I have been uninstalling and installing SQL server and visual studio for more then 3 times taking me almost 2 day. But, your solution helped me in less than 1 min. – Akang Toshi Mar 19 '22 at 15:03
  • This just worked like magic. Thanks a lot. I have spent a whole day trying to figure out this error. – Benjamin Ezugwu Jun 18 '22 at 15:20
  • 1
    FYI, you can use `sqllocaldb stop mssqllocaldb -k` to force stop a misbehaving instance. – wags1999 Oct 12 '22 at 21:04
  • OMG I deleted all my localdb databases. You should have put a BIG warning! – Felipe Costa Gualberto Oct 23 '22 at 12:56
3

For this particular error, what gave me access to my MDF in VS2019 was:

  1. In Solution Explorer, right click your MDF file
  2. Detach

That was it and I now have access. I was expecting to detach and attach, but that wasn't needed.

I also could not get to my (localdb) in SSMS either, so what helped me there was a solution by Leniel Maccaferri. Here is the link to his site, along with the excerpt that helped me:

https://www.leniel.net/2014/02/localdb-sqlserver-2012-cannot-open-database-requested-by-login-the-login-failed-error-4060.html

enter image description here

So guess what: the solution is ridiculously easy once you know what to do of course…

Click that Options >> button in Figure 1. Now select the Connection Properties tab.

SSMS Connect to Server | Connection Properties | Connect to database optionFigure 2 - SSMS Connect to Server | Connection Properties | Connect to database option

I had to type master in Connect to database field since I did not have it in the list of available databases.

Now click connect and you’re done.

James Lee
  • 754
  • 8
  • 13
  • 1
    Worth mentioning to check "Additional Connection Parameters" too in case you're an idiot like me and had a database name being set in there too! – benmccallum Oct 07 '21 at 06:10