Connect to a SQL database with Visual Studio Code

SQL Server Oct 12, 2021

Does Visual Studio connect to SQL Server?

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, and YES, it even lets you connect to a SQL database and query it.


To use VS Code to connect to your SQL servers, you must install the extension.  This is called SQL Server (mssql).

SQL Server VS Code Extension
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 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; you can click on that to start adding connections for your SQL servers.

When you click on this, a dialogue box will appear, asking for some details about your SQL server.  This will be the hostname and instance or your Azure SQL server URL.

It would be best to input your SQL server hostname and Instance name first.

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

The next box asks for an authentication type: a SQL Login, Integrated or Azure Active Directory (with MFA).

Once you've inputted that info, the next question you are asked is for 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.

Visual Studio Code
Visual Studio Code

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 it, select Execute Query, or use the shortcut CTRL+SHIFT+E.

Visual Studio Code performing SQL queries
Visual Studio Code performing SQL queries

Why VS Code over SQL Management Studio?

What I love about interacting with SQL inside VS Code is I have VS Code installed on my machine; it's always there.  I used it for a lot of reasons. I haven't got SQL Management Studio installed on my device 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 to using VS Code versus SQL Management Studio, but I've not found any for my needs.  

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


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.