Code Visual Studio Code



Visual Studio dev tools & services make app development easy for any platform & language. Try our Mac & Windows code editor, IDE, or Azure DevOps for free. Visual Studio Command Line When you install VS Code, the code command is available globally in your command line. This is very useful to start the editor and open a new window with the content of the current folder, with code.-n will create a new window. Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and available on your favorite platform.

Code-->

Applies to: SQL Server (all supported versions) - Linux

This article shows how to use the mssql extension for Visual Studio Code to develop SQL Server databases. Because Visual Studio Code is cross-platform, you can use mssql extension on Linux, macOS, and Windows.

Install and start Visual Studio Code

Visual Studio Code is a cross-platform, graphical code editor that supports extensions.

  1. Download and install Visual Studio Code on your machine.

  2. Start Visual Studio Code.

    Note

    If Visual Studio Code does not start when you are connected through an xrdp remote desktop session, see VS Code not working on Ubuntu when connected using XRDP.

Install the mssql extension

The mssql extension for Visual Studio Code lets you connect to a SQL Server, query with Transact-SQL (T-SQL), and view the results.

  1. In Visual Studio Code, select View > Command Palette, or press Ctrl+Shift+P, or press F1 to open the Command Palette.

  2. In the Command Palette, select Extensions: Install Extensions from the dropdown.

  3. In the Extensions pane, type mssql.

  4. Select the SQL Server (mssql) extension, and then select Install.

  5. After the installation completes, select Reload to enable the extension.

Create or open a SQL file

The mssql extension enables mssql commands and T-SQL IntelliSense in the code editor when the language mode is set to SQL.

  1. Select File > New File or press Ctrl+N. Visual Studio Code opens a new Plain Text file by default.

  2. Select Plain Text on the lower status bar, or press Ctrl+K > M, and select SQL from the languages dropdown.

    Note

    If this is the first time you have used the extension, the extension installs supporting SQL Server tools.

If you open an existing file that has a .sql file extension, the language mode is automatically set to SQL.

Connect to SQL Server

Follow these steps to create a connection profile and connect to a SQL Server.

  1. Press Ctrl+Shift+P or F1 to open the Command Palette.

  2. Type sql to display the mssql commands, or type sqlcon, and then select MS SQL: Connect from the dropdown.

    Note

    A SQL file, such as the empty SQL file you created, must have focus in the code editor before you can execute the mssql commands.

  3. Select the MS SQL: Manage Connection Profiles command.

  4. Then select Create to create a new connection profile for your SQL Server.

  5. Follow the prompts to specify the properties for the new connection profile. After specifying each value, press Enter to continue.

    Connection propertyDescription
    Server name or ADO connection stringSpecify the SQL Server instance name. Use localhost to connect to a SQL Server instance on your local machine. To connect to a remote SQL Server, enter the name of the target SQL Server, or its IP address. To connect to a SQL Server container, specify the IP address of the container's host machine. If you need to specify a port, use a comma to separate it from the name. For example, for a server listening on port 1401, enter <servername or IP>,1401.
    As an alternative, you can enter the ADO connection string for your database here.
    Database name (optional)The database that you want to use. To connect to the default database, don't specify a database name here.
    Authentication TypeChoose either Integrated or SQL Login.
    User nameIf you selected SQL Login, enter the name of a user with access to a database on the server.
    PasswordEnter the password for the specified user.
    Save PasswordPress Enter to select Yes and save the password. Select No to be prompted for the password each time the connection profile is used.
    Profile Name (optional)Type a name for the connection profile, such as localhost profile.

    After you enter all values and select Enter, Visual Studio Code creates the connection profile and connects to the SQL Server.

    Tip

    If the connection fails, try to diagnose the problem from the error message in the Output panel in Visual Studio Code. To open the Output panel, select View > Output. Also review the connection troubleshooting recommendations.

  6. Verify your connection in the lower status bar.

As an alternative to the previous steps, you can also create and edit connection profiles in the User Settings file (settings.json). To open the settings file, select File > Preferences > Settings. For more information, see Manage connection profiles.

Create a SQL database

  1. In the new SQL file that you started earlier, type sql to display a list of editable code snippets.

  2. Select sqlCreateDatabase.

  3. In the snippet, type TutorialDB to replace 'DatabaseName':

  4. Press Ctrl+Shift+E to execute the Transact-SQL commands. View the results in the query window.

    Tip

    You can customize the shortcut keys for the mssql commands. See Customize shortcuts.

Create a table

  1. Delete the contents of the code editor window.

  2. Press Ctrl+Shift+P or F1 to open the Command Palette.

  3. Type sql to display the mssql commands, or type sqluse, and then select the MS SQL: Use Database command.

  4. Select the new TutorialDB database.

  5. In the code editor, type sql to display the snippets, select sqlCreateTable, and then press Enter.

  6. In the snippet, type Employees for the table name.

  7. Press Tab to get to the next field, and then type dbo for the schema name.

  8. Replace the column definitions with the following columns:

  9. Press Ctrl+Shift+E to create the table.

Insert and query

  1. Add the following statements to insert four rows into the Employees table.

    While you type, T-SQL IntelliSense helps you to complete the statements:

    Tip

    The mssql extension also has commands to help create INSERT and SELECT statements. These were not used in the previous example.

  2. Press Ctrl+Shift+E to execute the commands. The two result sets display in the Results window.

View and save the result

  1. Select View > Editor Layout > Flip Layout to switch to a vertical or horizontal split layout.

  2. Select the Results and Messages panel headers to collapse and expand the panels.

    Tip

    You can customize the default behavior of the mssql extension. See Customize extension options.

  3. Select the maximize grid icon on the second result grid to zoom in to those results.

    Note

    The maximize icon displays when your T-SQL script produces two or more result grids.

  4. Open the grid context menu by right-clicking on the grid.

  5. Select Select All.

  6. Open the grid context menu again and select Save as JSON to save the result to a .json file.

  7. Specify a file name for the JSON file.

  8. Verify that the JSON file saves and opens in Visual Studio Code.

If you need to save and run SQL scripts later, for administration or a larger development project, save the scripts with a .sql extension.

Next steps

If you're new to T-SQL, see Tutorial: Write Transact-SQL statements and the Transact-SQL Reference (Database Engine).

Visual Code Studio Code Format

For more information on using or contributing to the mssql extension, see the mssql extension project wiki.

For more information on using Visual Studio Code, see the Visual Studio Code documentation.

-->

When debugging a .NET application, you may find that you want to view source code that you don't have. For example, breaking on an exception or using the call stack to navigate to a source location.

Note

  • Source code generation (decompilation) is only available for .NET applications and is based on the open source ILSpy project.
  • Decompilation is only available in Visual Studio 2019 16.5 and later.
  • Applying the SuppressIldasmAttribute attribute to an assembly or module prevents Visual Studio from attempting decompilation.

Generate source code

When you're debugging and no source code is available, Visual Studio shows the Source Not Found document, or if you don’t have symbols for the assembly, the No Symbols Loaded document. Both documents have a Decompile source code option that generates C# code for the current location. The generated C# code can then be used just like any other source code. You can view the code, inspect variables, set breakpoints, and so on.

No symbols loaded

The following illustration shows the No Symbols Loaded message.

Source not found

The following illustration shows the Source Not Found message.

Generate and embed sources for an assembly

In addition to generating source code for a specific location, you can generate all the source code for a given .NET assembly. To do this, go to the Modules window and from the context menu of a .NET assembly, and then select the Decompile source code command. Visual Studio generates a symbol file for the assembly and then embeds the source into the symbol file. In a later step, you can extract the embedded source code.

Extract and view the embedded source code

You can extract source files that are embedded in a symbol file using the Extract Source Code command in the context menu of the Modules window.

The extracted source files are added to the solution as miscellaneous files. The miscellaneous files feature is off by default in Visual Studio. You can enable this feature from the Tools > Options > Environment > Documents > Show Miscellaneous files in Solution Explorer checkbox. Without enabling this feature, you won't be able to open the extracted source code.

Extracted source files appear in the miscellaneous files in Solution Explorer.

Known limitations

Requires break mode

Code Visual Studio Code

Generating source code using decompilation is only possible when the debugger is in break mode and the application is paused. For example, Visual Studio enters break mode when it hits a breakpoint or an exception. You can easily trigger Visual Studio to break the next time your code runs by using the Break All command ().

Decompilation limitations

Visual studio code tutorial

Generating source code from the intermediate format (IL) that is used in .NET assemblies has some inherent limitations. As such, the generated source code doesn't look like the original source code. Most of the differences are in places where the information in the original source code isn't needed at runtime. For example, information such as whitespace, comments, and the names of local variables aren't needed at runtime. We recommend that you use the generated source to understand how the program is executing and not as a replacement for the original source code.

Debug optimized or release assemblies

When debugging code that was decompiled from an assembly that was compiled using compiler optimizations, you may come across the following issues:

  • Breakpoints may not always bind to the matching sourcing location.
  • Stepping may not always step to the correct location.
  • Local variables may not have accurate names.
  • Some variables may not be available for evaluation.

More details can be found in the GitHub issue: ICSharpCode.Decompiler integration into VS Debugger.

Microsoft Studios Visual Code

Decompilation reliability

A relatively small percentage of decompilation attempts may result in failure. This is due to a sequence point null-reference error in ILSpy. We have mitigated the failure by catching these issues and gracefully failing the decompilation attempt.

More details can be found in the GitHub issue: ICSharpCode.Decompiler integration into VS Debugger.

Visual studio code

Limitations with async code

The results from decompiling modules with async/await code patterns may be incomplete or fail entirely. The ILSpy implementation of async/await and yield state-machines is only partially implemented.

More details can be found in the GitHub issue: PDB Generator Status.

Just My Code

The Just My Code (JMC) settings allows Visual Studio to step over system, framework, library, and other non-user calls. During a debugging session, the Modules window shows which code modules the debugger is treating as My Code (user code).

Decompilation of optimized or release modules produces non-user code. If the debugger breaks in your decompiled non-user code, for example, the No Source window appears. To disable Just My Code, navigate to Tools > Options (or Debug > Options) > Debugging > General, and then deselect Enable Just My Code.

Extracted sources

Source code extracted from an assembly has the following limitations:

  • The name and location of the generated files isn't configurable.
  • The files are temporary and will be deleted by Visual Studio.
  • The files are placed in a single folder and any folder hierarchy that the original sources had isn't used.
  • The file name for each file contains a checksum hash of the file.

Generated code is C# only

Decompilation only generates source code files in C#. There is no option to generate files in any other language.