|
| 1 | +--- |
| 2 | +title: "Quickstart: Connect To and Query a Database With the MSSQL Extension for Visual Studio Code" |
| 3 | +description: Learn how to connect to a database using the MSSQL extension for Visual Studio Code, and execute Transact-SQL (T-SQL) statements to interact with your database. |
| 4 | +author: croblesm |
| 5 | +ms.author: roblescarlos |
| 6 | +ms.reviewer: maghan, randolphwest |
| 7 | +ms.date: 11/20/2024 |
| 8 | +ms.service: sql |
| 9 | +ms.subservice: tools-other |
| 10 | +ms.topic: quickstart |
| 11 | +--- |
| 12 | + |
| 13 | +# Quickstart: Connect to and query a database with the MSSQL extension for Visual Studio Code |
| 14 | + |
| 15 | +In this quickstart, you learn how to use the MSSQL extension for Visual Studio Code to connect to a database, whether it's running locally, in a container, or in the cloud. Then you learn how to use Transact-SQL (T-SQL) statements to create a database, define a table, insert data, and query results. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +To complete this quickstart, you must have: |
| 20 | + |
| 21 | +- **Visual Studio Code:** If you don't have Visual Studio Code installed, download and install it from the [Official Visual Studio Code website](https://code.visualstudio.com/). |
| 22 | + |
| 23 | +- **MSSQL extension for Visual Studio Code:** In Visual Studio Code, open the Extensions view by selecting the Extensions icon in the Activity Bar on the side of the window. Search for `mssql` and select **Install** to add the extension. |
| 24 | + |
| 25 | +- **Access to a database:** If you don't have access to a database instance, you can use one by selecting one of the following options: |
| 26 | + |
| 27 | + - **Containerized SQL Server:** Run SQL Server in a Docker container for easy setup and portability. For more information, see [Quickstart: Run SQL Server Linux container images with Docker](../../../linux/quickstart-install-connect-docker.md). |
| 28 | + |
| 29 | + - **Azure SQL Database:** If you prefer a cloud-based option, create a free Azure account and set up an Azure SQL Database. For more information, see [Quickstart: Create a single database - Azure SQL Database](/azure/azure-sql/database/single-database-create-quickstart). |
| 30 | + |
| 31 | + - **SQL database in Fabric (preview):** If you need a simple, autonomous and secure, and optimized for AI database, create a SQL database in Fabric (preview). For more information, see [Create a SQL database in Microsoft Fabric](/fabric/database/sql/tutorial-create-database). |
| 32 | + |
| 33 | + - **Local SQL Server:** Alternatively, download and install SQL Server 2022 Developer Edition on your local machine. For more information, see [Microsoft SQL Server website](https://www.microsoft.com/sql-server/sql-server-downloads). |
| 34 | + |
| 35 | + - **Azure SQL Managed Instance:** If you need a fully managed SQL Server instance, create an Azure SQL Managed Instance. For more information, see [Quickstart: Create Azure SQL Managed Instance](/azure/azure-sql/managed-instance/instance-create-quickstart). |
| 36 | + |
| 37 | +> [!NOTE] |
| 38 | +> If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/). |
| 39 | +
|
| 40 | +## Connect to a database instance |
| 41 | + |
| 42 | +1. **Start Visual Studio Code:** Open the MSSQL extension for Visual Studio Code** by selecting the server viewlet on the left side of the window or by pressing `Ctrl+Alt+D`. |
| 43 | + |
| 44 | + The first time you run the MSSQL extension for Visual Studio Code, the **Enable Experiences and Reload** button appears when the extension is loaded for the first time. |
| 45 | + |
| 46 | +1. **Connect to database:** |
| 47 | + |
| 48 | + This article uses the **Parameter** input type and **SQL Login** for the authentication type. |
| 49 | + |
| 50 | + Follow the prompts to specify the properties for the new connection profile. Complete each field as follows: |
| 51 | + |
| 52 | + | Connection property | Value | Description | |
| 53 | + | --- | --- | --- | |
| 54 | + | **Profile Name** (optional) | Leave this field blank. | Type a name for the connection profile. such as *localhost profile*. | |
| 55 | + | **Server name** | Enter the server name here. Ex: localhost | Specify 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`.<br /><br />By default, the connection string uses port 1433. A default instance of SQL Server uses 1433 unless modified. If your instance is listening on 1433, you don't need to specify the port.<br />As an alternative, you can enter the ADO connection string for your database here. | |
| 56 | + | **Trust Server Certificate** | Check this field. | Select this option to trust the server certificate. | |
| 57 | + | **Input type** | Parameter. | Choose from **Parameter**, **Connection String**, or **Browse Azure**. | |
| 58 | + | **Database name** (optional) | \<Default>. | The database that you want to use. To connect to the default database, don't specify a database name here. | |
| 59 | + | **Authentication Type** | SQL login. | Choose either **SQL Login**, **Windows Authentication**, or **Microsoft Entra ID**. | |
| 60 | + | **User name** | Enter your *\<username\>* for the SQL Server. | If you selected **SQL Login**, enter the name of a user with access to a database on the server. | |
| 61 | + | **Password** | Enter your *\<password\>* for the SQL Server. | Enter the password for the specified user. | |
| 62 | + | **Save Password** | Check this field to save the password for future connections. | Press **Enter** to select **Yes** and save the password. Select **No** to be prompted for the password each time the connection profile is used. | |
| 63 | + | **Encrypt** | **Mandatory**. | Choose from **Yes**, **No**, or **Mandatory**. | |
| 64 | + |
| 65 | + After you enter all values and select **Enter**, Visual Studio Code creates the connection profile and connects to the SQL Server. |
| 66 | + |
| 67 | + :::image type="content" source="media/connect-database-visual-studio-code/mssql-connection-dialog-parameters.png" alt-text="Screenshot of the Connection Dialog window." lightbox="media/connect-database-visual-studio-code/mssql-connection-dialog-parameters.png"::: |
| 68 | + |
| 69 | +## Create a database |
| 70 | + |
| 71 | +Let's start by creating a database called `Library`. |
| 72 | + |
| 73 | +1. Open a new query editor: Press `Ctrl+N` to open a new query editor, or right-click on your server and select **New Query**. |
| 74 | + |
| 75 | +1. Create the database by pasting the following snippet into the query editor and selecting **Run**: |
| 76 | + |
| 77 | + ```sql |
| 78 | + IF NOT EXISTS (SELECT name |
| 79 | + FROM sys.databases |
| 80 | + WHERE name = N'Library') |
| 81 | + CREATE DATABASE Library; |
| 82 | + ``` |
| 83 | + |
| 84 | + This script creates a new database called `Library` if it doesn't already exist. |
| 85 | + |
| 86 | +The new `Library` database appears in the list of databases. If you don't see it immediately, refresh the Object Explorer. |
| 87 | + |
| 88 | +## Create a table |
| 89 | + |
| 90 | +Now, let's create the `Authors` table within the `Library` database. |
| 91 | + |
| 92 | +1. Open a new query editor and make sure the connection context is set to the `Library` database. |
| 93 | + |
| 94 | +1. The following code creates the `Authors` table with an `IDENTITY` column for the primary key. Replace the text in the query window with the following snippet and select **Run**: |
| 95 | + |
| 96 | + ```sql |
| 97 | + CREATE TABLE dbo.Authors |
| 98 | + ( |
| 99 | + id INT IDENTITY (1, 1) NOT NULL PRIMARY KEY, |
| 100 | + first_name NVARCHAR (100) NOT NULL, |
| 101 | + middle_name NVARCHAR (100) NULL, |
| 102 | + last_name NVARCHAR (100) NOT NULL |
| 103 | + ); |
| 104 | + ``` |
| 105 | + |
| 106 | +This script creates the `Authors` table with an `IDENTITY` column for the `id`, which automatically generates unique IDs. |
| 107 | + |
| 108 | +## Insert rows |
| 109 | + |
| 110 | +Now, let's insert some data into the `Authors` table. |
| 111 | + |
| 112 | +1. Replace the text in the query window with the following snippet and select **Run**: |
| 113 | + |
| 114 | + ```sql |
| 115 | + INSERT INTO dbo.Authors (first_name, middle_name, last_name) |
| 116 | + VALUES ('Isaac', 'Yudovick', 'Asimov'), |
| 117 | + ('Arthur', 'Charles', 'Clarke'), |
| 118 | + ('Herbert', 'George', 'Wells'), |
| 119 | + ('Jules', 'Gabriel', 'Verne'), |
| 120 | + ('Philip', 'Kindred', 'Dick'); |
| 121 | + ``` |
| 122 | + |
| 123 | +As a result, the sample data is added to the `Authors` table. |
| 124 | + |
| 125 | +## View the data |
| 126 | + |
| 127 | +To verify the data in the `Authors` table, run the following query: |
| 128 | + |
| 129 | +```sql |
| 130 | +SELECT * |
| 131 | +FROM dbo.Authors; |
| 132 | +``` |
| 133 | + |
| 134 | +This query returns all records in the `Authors` table, showing the data you inserted. |
| 135 | + |
| 136 | +## Related content |
| 137 | + |
| 138 | +- [What is the MSSQL extension for Visual Studio Code?](mssql-extension-visual-studio-code.md) |
| 139 | +- [Tutorial: Write Transact-SQL statements](../../../t-sql/tutorial-writing-transact-sql-statements.md) |
| 140 | +- [Learn more about contributing to the mssql extension](https://github.com/Microsoft/vscode-mssql/wiki) |
| 141 | +- [What is the local development experience for Azure SQL Database?](/azure/azure-sql/database/local-dev-experience-overview) |
0 commit comments