SQL Server Express LocalDB via JDBC
I was working on a project recently that uses SQL Server in the deployed environments, and SQL Server Express LocalDB for local dev, and I wanted to connect to it with Jetbrains DataGrip instead of SQL Server Management Studio.
Update 2020-02-25
Well actually, it kind of is pretty easy. I’m not sure when DataGrip gained improved support for SqlLocalDB, but connecting to a localdb instance is now fairly straightforward as of DataGrip 2019.3 (and probably earlier). You can find the latest instructions at https://www.jetbrains.com/help/datagrip/connecting-to-sql-server-express-localdb.html
Unlike the original post below, you no longer need to mess around with the named pipe nonsense, DataGrip will discover the available databases for you.
Original post continues below
Turns out that’s not so straightforward 😒
- DataGrip is a Java-based application and uses JDBC to connect to databases.
- LocalDB only supports named pipes, it doesn’t support TCP/IP, not even from
localhost
. - The Microsoft mssql-jdbc driver doesn’t support named pipes and therefore can’t connect to LocalDB, see the Github discussion.
¯\_(ツ)_/¯
This solution is from this Stackoverflow answer, I just pulled together some of the context around what the problem is and why the solution is still a thing.
Enter the jTDS driver. It does support named pipes, but it’s really old and hasn’t seen any activity for years. That said, I found it to work for my purposes, and it might for yours too.
As the SO answer says, a LocalDB JDBC connection string looks like this:
jdbc:jtds:sqlserver://./DatabaseName;instance=LOCALDB#88893A09;namedPipe=true
Change DatabaseName
to your database name, and LOCALDB#xxxxxxxx
to the name of your LocalDB named pipe.
Obviously you need to find the name of the named pipe. First up, find the name of your LocalDB instance, then get its info
;
> SqlLocalDb info
MSSQLLocalDb
> SqlLocalDb info MSSQLLocalDb
Name: mssqllocaldb
Version: 13.1.4001.0
Shared name:
Owner: ***snip***
Auto-create: Yes
State: Stopped
Last start time: 9/12/2019 11:22:29 AM
Instance pipe name:
My LocalDB is stopped, so start it.
> SqlLocalDB start MSSQLLocalDB
LocalDB instance "mssqllocaldb" started.
>SqlLocalDB.exe info MSSQLLocalDB
Name: mssqllocaldb
Version: 13.1.4001.0
Shared name:
Owner: ***snip***
Auto-create: Yes
State: Running
Last start time: 10/12/2019 4:22:08 PM
Instance pipe name: np:\\.\pipe\LOCALDB#C5287F00\tsql\query
Now that the instance is started, we can ge the named pipe details, in this case, np:\\.\pipe\LOCALDB#C5287F00\tsql\query
So we can now construct the jdbc connection string;
jdbc:jtds:sqlserver://./MyDatabaseName;instance=LOCALDB#C5287F00;namedPipe=true
Finally, in DataGrip, create the database connection.