Connect to Azure SQL Database Using VS Code

Visual Studio Code (VS Code) is an excellent tool for connecting to Azure SQL Database and managing data right from within your development environment. With a few simple steps, you can run queries, explore tables, and integrate database tasks into your development workflow.

This article walks you through connecting to Azure SQL using VS Code.

Install the SQL Server (mssql) Extension

Microsoft provides the SQL Server (mssql) extension that enables us to work with SQL Server, Azure SQL, and SQL databases in Fabric, right from within VS Code. So if we’re going to use VS Code to connect to Azure SQL Database, we’ll need this extension.

If you haven’t already installed the SQL Server (mssql) extension, go ahead and do that now. This is very easy. You can do it in three steps (below).

Open VS Code and do this:

  1. Go to Extensions by clicking the Extensions icon in the activity bar on the side of the window
    (or use the keyboard shortcut: Ctrl+Shift+X on Windows/Linux, Cmd+Shift+X on macOS)
  2. Search for “SQL Server (mssql)”
  3. Install the official Microsoft extension (by clicking the Install button at the top, near the extension’s info)
Screenshot of the SQL Server (mssql) extension in VS Code

That’s it. You can now work with MSSQL databases in VS Code.

Connect to Your Database

Now that the SQL Server (mssql) extension is installed, let’s go ahead and connect to Azure.

  1. Open the Command Palette by selecting View > Command Palette… from the top menu
    (or use the keyboard shortcut: Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS).
  2. Add Connection: Type “MS SQL: Add Connection” and select it.

This should eventually result in the “Connect to Database” screen appearing:

Screenshot of the "Connect to Database" screen

Go ahead and fill in the connection details. For example:

  • Profile Name: Give it a name (e.g., “Azure Dev DB”)
  • Connection Group: It’s fine to leave it at the default (Connection Group is just a way to organize your saved connections within the SQL Server extension in VS Code).
  • Input type: Parameters
  • Server name: yourserver.database.windows.net
  • Trust server certificate: Leave it unchecked.
  • Authentication Type: Select “SQL Login”
  • User name: Your admin username
  • Password: Your admin password
  • Save Password: Choose “Yes”
  • Database name: dev-test-db
  • Encrypt: Strict

You may need to use different settings if you have a reason to do so (for example, if you use a different Authentication type).

Once done, click “Connect”.

Once connected, you should be able to see the Azure connection in the Connections pane in VS Code:

Screenshot of VS Code's "Connections" pane, with a connection to our Azure SQL database

Run Queries

You can now write queries agains your Azure SQL Database.

To run a query, right click on the Azure database and select “New Query”:

Screenshot of the "New Query" option

That opens a new query tab. In the query tab, run a simple SQL query to test it out.

For example:

SELECT @@VERSION as SQLVersion;

To run the query, click the triangular “Execute Query” button at the top.

Here’s what I got:

Screenshot of the query tab and result

And that’s it. You have now connected to your Azure SQL database.