Connection Strings for LocalDB

Motivation – Why LocalDB

While watching a course on Pluralsight and coding along I found myself needing a database that Entity Framework could connect to. My DbContext class had a DropCreateDatabaseIfModelChanges method and some test data to store but since I hadn’t re-installed MS SQL Express since doing some maintenance on my PC I didn’t have a database I could immediately connect to.

Not wanting to lose momentum on the course I looked for a quicker option which turned out to be MS SQL Server LocalDB. Knowing that the 2016 version of SQL Server was just release a few weeks ago I decided to go find the download link for LocalDB 2016. Unfortunately it doesn’t exist yet and the SQL Server Express 2016 download page only links to the full installer at the moment.  I downloaded the installer hoping I could just install LocalDB but I did not see any options so I abandoned this approach for the time being.

Fortunately the 2014 installer page provides a modal upon clicking Download that gives a select of options to determine exactly which combination or individual tools to download. I downloaded the x64 LocalDB installer and ended up repairing the install because I apparently already had LocalDB installed.

Connection Strings

My connection string for my ASP.NET app needed some blanks filled in because  wasn’t going to cut it.

I knew the data source should look something like (localdb)\v11.0 or localdb\someInstance but I wasn’t exactly sure. I found this page which describes how to work with LocalDB which confirmed my suspicions but I still didn’t know what my instance name was and all the ones I was trying were throwing errors.

SqlLocalDB.exe

I then did some more searching and found a reference to using the SqlLocalDB.exe application as a command line tool to tell me what instances I had available and ensure those instances were online.

I opened up PowerShell and navigated to  C:\Program Files\Microsoft SQL Server\> where I had a couple version options available. I went with the highest one ( 130) and navigated to \Tools\Binn.

In this folder is the  .\SqlLocalDB.exe application which, if entered by itself, will list the command line options that are available. I ran the following commands.

With the following being the explanations for the options

start|s ["instance name"]
Starts the LocalDB instance with the specified name

info|i
Lists all existing LocalDB instances owned by the current user
and all shared LocalDB instances.

info|i "instance name"
Prints the information about the specified LocalDB instance.

versions|v
Lists all LocalDB versions installed on the computer.

So at this point I knew that my instance name was  MSSQLLocalDB, it was started and running and I should be able to connect to it via  (localdb)\MSSQLLocalDB.

I opened up LINQPad and added a new database connection, put in the above data source and everything worked! A few minutes later my demo code was up and running.


Leave a Reply