Applies to: Visual Studio Code Version 1.3.1
It took me about 6 months but I finally stopped using the PowerShell ISE and now only use Visual Studio Code and the PowerShell console. I’ve been working with SQL a lot and in the middle of writing some T-SQL a thought popped into my head, “I wonder if I can connect to SQL with Visual Studio Code?” As it turns out you can and there is a nice extension for connecting to Microsoft SQL called vscode-mssql. There are a few more extensions available, but I chose this one based on reviews and by the documentation available at the time.
Install the vscode-mssql Extension
To install the extension follow the steps defined below.
- Open Visual Studio Code…
- Hit Ctrl+Shift+X
- Type sql in the search box
- Click on vscode-mssql
- Click Install
- Click Enable
Setting up Connections
Next you’ll want to setup some connections to your SQL databases. To do that you will need to modify either the User Settings or Workspace Settings in Visual Studio code. I chose to modify the User Settings.
- In Visual Studio Code Click on File
- Click Preferences
- Click User Settings
- Within the settings.json file define your databases settings, see below for details.
Connect to the Database
With the connections settings defined you can now use the extension to issue T-SQL command against the database, but before they will work you have to connect to the database.
- Hit F1
- Type mssql
- Hit Enter
- Select the database you want, then hit Enter
Issuing T-SQL statements within Visual Studio Code
Now that you’ve got the extension install and the database connections defined, you can now issue some T-SQL statements. You’ve got a few options at this point. You can either open up a .sql file or you can type out some sql code. Below will walk through writing some T-SQL code and then executing it. If you open a .sql file skip to line 4.
- Ctrl+N for a new file
- Type your T-SQL statement [select * from Users]
- Hit Ctrl+K then hit M and type SQL and hit enter [Changes language syntax to SQL]
- Hit Ctrl+Shift+E [Exectues the T-SQL]