1

I would like to be able to write a simple script to be able to drop a localdb database at will.

First I have to stop the instance to kill any active connections by running this in the VS package manager

SqlLocalDB.exe stop v11.0

Then I need to drop the db which I have to do by connecting to (localdb)\v11.0 master db with Linqpad or SSMS

DROP DATABASE MyDb

But I don't want to switch windows to do this, I'd like to just write a script. I can't figure out how to execute the second query from the command line. I've tried this

SQLCMD.EXE -s "(localdb)\v11.0" -d "master" -q "drop database MyDb"

which gives me an error

SQLCMD.EXE : Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Named Pipes Provider: 
Could not open a connection to SQL Server [2]. .
George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • You just stopped the instance, so of course sqlcmd cannot connect to it. – ErikEJ Apr 28 '15 at 05:53
  • @ErikEJ I believe localdb starts up when you try to connect to it. Also I should clarify that *all* queries (even when I don't stop the instance) fail with the same error message. I must be doing something wrong – George Mauer Apr 28 '15 at 14:51
  • Are you sure you are using a "new" version of sqlcmd.exe? – ErikEJ Apr 28 '15 at 16:42
  • @ErikEJ `PM> SQLCMD.EXE -? / Microsoft (R) SQL Server Command Line Tool / Version 11.0.2100.60 NT x64`. Are you saying that what I have above should work? – George Mauer Apr 28 '15 at 19:29
  • Maybe use a capital letter "-S"? http://stackoverflow.com/questions/14197802/how-to-connect-sqlcmd-to-the-server – jjj Apr 28 '15 at 20:12

2 Answers2

2

It looks like it should work if you use a capital letter "S"

SQLCMD.EXE -S "(localdb)\v11.0" -d "master" -Q "drop database MyDb"

You'd probably also want to use a capital "Q" so that sqlcmd will exit immediately.

https://msdn.microsoft.com/en-us/library/ms162773.aspx

jjj
  • 4,822
  • 1
  • 16
  • 39
  • Ah, yes now I can connect. Still having issues with "Login failed for user" (which is weird, I assumed it would use integrated security), but that's another issue. – George Mauer Apr 29 '15 at 13:40
1

As of May 2020, you can just type:

Drop-Database

in the Package Manager console and it should work:

Are you sure you want to perform this action?
Performing the operation "Drop-Database" on target "database 'databasename' on server '(localdb)\mssqllocaldb'".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):y

If it doesn't work, you might need to manually install Entity Framework Core tools.

Cesar
  • 2,059
  • 25
  • 30