Connect to a SQL database with Visual Studio Code

SQL Server Oct 12, 2021

I'm a big fan of Visual Studio Code or VS Code as it's sometimes referred to.  It's such a powerful tool, allowing you to write Markdown files, write your code, deploy services to Azure or even connect to a SQL database and query it.

Extension

To use VS Code to connect to your SQL servers you need to have the extension installed.  This is called SQL Server (mssql).

SQL Server VS Code Extension

Once the extension is installed you will see a new icon down the left hand menu, it looks like a server icon.  If you click on this you can start to create connections to your SQL servers.

Create connections

When you are in the extension blade you will see an "Add Connection" line, this is interactive and you can click on that to start to add connections for your SQL servers.

A dialog box will appear when you click on this asking for some details around your SQL server.  This will either be the host name and instance or your Azure SQL server URL.

You need to input your SQL server host name and Instance name first.

Then you have the option to input a database name or not.

The next box is asking for a authentication type, this can be a SQL Login, Integrated or Azure Active Directory (with MFA).

Once you've inputted that info the next question you are asked if a display name or profile name for this connection.  Make sure you call it something recognisable for the future and you want to connect to that SQL server. 😉

Once you've completed this process you should see the connection down the left hand side and be able to interact with the SQL Server.

If you right click on your database you will have a blank page to create a query.  Once you've written the query you can right click on it and select Execute Query or use the shortcut CTRL+SHIFT+E.

Why VS Code over SQL Management Studio?

What I love about being able to interact with SQL inside VS Code is I have VS Code installed on my machine, it's there, always.  I used it for a lot of reasons. I haven't got SQL Management Studio installed on my machine these days.  So I have easy access to work with a SQL database, both a local install or an Azure one whenever I need without any hassle.

I'm not a big SQL person, and I'm not an expert by any means at all.   So there might be some limitations of using VS Code versus SQL Management Studio but I've not found any for my needs.  

Do give the VS Code and SQL combination a try and let me know what you think of it and if you find any gotchas or great tips, I'd love to hear them!

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.