Connect to a SQL database with Visual Studio Code

Join me as I look at how you can use Visual Studio Code to connect to a SQL database and interact with it.

Connect to a SQL database with Visual Studio Code
Connect to a SQL database with Visual Studio Code

Explore the seamless integration of Visual Studio Code (VS Code) with SQL Server in this blog post. Learn how to install the SQL Server extension, create connections to your SQL servers, and leverage VS Code's capabilities for writing and executing SQL queries. Discover the convenience of using VS Code for SQL interactions, even in the absence of SQL Management Studio, and share your insights or tips for this dynamic combination.

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 will even let you connect to a SQL database and query it.

Extension

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!