MSSQL Extension For Visual Studio Code

Introduction

Visual Studio Code is a graphical code editor for Linux, macOS and Windows that supports various extensions. Personally, I think in current scenarios, Visual Studio Code is the best code editor tool. In Visual Studio Code, you can get an extension about AngularJS, NodeJS, TypeScript, C#, Python and also for other languages. In this article, I will explain how can we use Visual Studio Code for SQL Server. We will create a connection to SQL Server and afterwards, we run various queries in the code editor.

If you don’t have Visual Studio Code, download Visual Studio Code.

Now, start your Visual Studio Code and type “mssql”. You will find an extension for “mssql” and download it.


This extension installs the files required for Microsoft SQL Server, Azure SQL and SQL Data Warehouse development .


After successful installation, create a “data.sql” file in your editor and ensure that language mode for your file is “SQL”.


In the current state, we are in disconnected mode. If we write any query, we will not get any result. First, we will create a connection to SQL Server. Press “Ctrl+Shift+P” or F1 and type “sql”. You will get a dropdown list. From this list, click on “MS SQL: Connect” option and select “Create Connection Profile”.


When you enter, then it will ask you about your “Server Name”, “database” (it is optional), user name, password and name for your profile. After answering all the points given above, Visual Studio makes the connection for your SQL Server instance. If the connection is successfully established, then you will find your server name and database name in status bar. You will get Master database, if you don’t define the database name during the connection establishment, otherwise you will get your database name.

 

Create database

During the connection, we don’t define the database, so we create a database. Write SQL in your SQL file, which you will create, when you type SQL, then you will get a list of code snippets.


Click on “sqlCreateDatabase” option. When you enter on this, you will get a code snippet to create a database. Write “testDatabase” for this exercise.

To execute the query given above, create the database and now press “Ctrl+ Shift +E”. You will get a message of the connection establishment, if the database is created successfully.


Use database

After creating the database, we use this database, so press “F1”, type “SQL” and select “Use Database” option.


 Now, select the “testDatabase” from the list.


Create a table

Now, type “sql” in your file and select the “sqlCreateTable” code snippets from this list. Create an Employee table, as shown below.




After writing all the column names and constraints, when you press “Ctrl+Shift+E”, an Employee table will be created. If you again press the “Ctrl+Shift+E”, then you will get an error that the table name already exists. Now, our table is ready. Let’s insert some values into Employee table.

Insert Data

Now, write some insert queries for employee table and press Ctrl+Shift +E.

  1. INSERT Into Employee  
  2. values(1, 'Pankaj', 22, 'Alwar');  
  3. INSERT Into Employee  
  4. values(2, 'Sandeep', 23, 'Jaipur');  
  5. INSERT Into Employee  
  6. values(3, 'Rahul', 24, 'Delhi');  
  7. INSERT Into Employee  
  8. values(4, 'Sanjeev', 22, 'Alwar');  

Select data from tables

Now, write “Select * From Employee” query into your terminal and execute this query. When you execute the query, you will get the data into grid format.


One of the great features of Visual Studio code is that we can save our table result into JSON and XML format. For this, right click on your table and select.


Save data into JSON format

Right click on the table and select “Save as JSON” option. Using this option, you can save your table data into JSON file.


CSV data into CSV format

Right click on the table and select “Save as CSV” option. Using this option, you can save your table data into a CSV file.


Copy and Copy with Header

When you right click on grid result, you will find option to Save, and Save with header. Using this option, you can copy the data without or with the data. You can select a single value, single row, single column or combination of all these.


Conclusion

Visual Studio Code is a great IDE for SQL Server and Azure, if you need to perform some SQL queries and don’t want to open SQL Server Management Studio for some simple queries, then you should prefer Visual Studio Code, as it will provide a great intelliSense for SQL queries.

原文链接:http://www.c-sharpcorner.com/article/mssql-extension-for-visual-studio-code/

转载于:https://www.cnblogs.com/sparkleDai/p/7604915.html

### 安装和配置 MySQL 插件 #### 在 VS Code 中安装 MySQL 扩展 为了能够在 Visual Studio Code (VS Code) 中高效地管理 MySQL 数据库,建议先通过官方市场安装合适的扩展工具。具体操作如下: 打开命令面板(快捷键 `Ctrl+Shift+P` 或者 `Cmd+Shift+P`),输入并选择 Extensions: Install Extension 命令,接着在搜索框内键入 "MySQL" 关键词来查找相关插件[^3]。 对于大多数开发者而言,“MySQL for Visual Studio” 和 “DataGrip Tools: Database Client & Query Builder” 是两个非常受欢迎的选择。前者提供了基本的数据浏览功能;后者则集成了更强大的查询构建器和其他辅助特性。 #### 配置 MySQL 连接参数 一旦选择了适合自己的 MySQL 插件之后,则需按照提示完成初步设定工作。通常情况下,这涉及到提供目标数据库实例的具体连接信息,比如主机地址、端口号、用户名及密码等必要字段。 以 DataGrip Tools 为例,在成功安装该插件后,可以通过左侧活动栏中的图标访问内置终端,并执行相应的初始化向导流程。期间会要求填写上述提到的各项细节以便建立有效的通信链路。 ```json { "mysql": { "host": "localhost", "port": 3306, "username": "root", "password": "", "database": "testdb" } } ``` 这段 JSON 片段展示了典型的 MySQL 连接字符串结构,实际应用时应依据个人环境做适当调整。 #### 测试连接有效性 当所有前期准备工作完成后,可以编写简单的 SQL 脚本来验证当前配置是否能够正常运作。例如,尝试运行一条显示现有表格列表的语句 `SHOW TABLES;` 来确认已成功接入指定的目标数据库。 ```sql SHOW TABLES; ``` 如果一切顺利的话,应该可以在编辑器下方的结果窗口看到预期返回的内容摘要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值