介绍 (Introduction)
Visual Studio Code (Code), have you heard of this product yet? This is an open-source, cross-platform and extremely light weight code editor from Microsoft. You may see some folks explain this editor as the little brother to Visual Studio Community Edition (VS Community), but it is more compared to editors like Atom, Sublime Text or even Notepad++. It is not something you can use to compile program code, so it is for the less complex coding projects. I utilize Code as my editor of choice now with PowerShell, and even T-SQL at times. In this article, I wanted to walk you through using Code and note some specific extensions I use for PowerShell and SQL Server.
Visual Studio代码(代码),您听说过此产品吗? 这是Microsoft提供的开源,跨平台且轻量级的代码编辑器。 您可能会看到一些人将此编辑器解释为Visual Studio Community Edition(VS社区)的弟弟,但与Atom,Sublime Text甚至Notepad ++等编辑器相比,它的作用更大。 您不能使用它来编译程序代码,因此它适用于不太复杂的编码项目。 现在,我将使用PowerShell作为我的首选编辑器,甚至有时使用T-SQL。 在本文中,我想引导您使用代码,并注意一些用于PowerShell和SQL Server的特定扩展。
基本布局 (Basic Layout)
Getting around Code is easy, once you get familiar with it. Just like any other new program it may seem overwhelming but I wanted to walk you through this as it helped me to understand how to use it more efficiently. You can see the screenshot below for a visual reference.
一旦熟悉了代码,就很容易做到。 就像其他任何新程序一样,它似乎不堪重负,但我想带您逐步了解它,因为它可以帮助我了解如何更有效地使用它。 您可以查看以下屏幕截图,以获取直观参考。
- View Bar – The side bar will collapse as you need more real estate to work. This bar offers the ability to open and close the side bar as you need. If the side bar is already open for the Explorer, clicking on that icon will cause the side bar to close. 视图栏–当您需要更多房地产才能工作时,侧栏会折叠。 该栏可根据需要打开和关闭侧栏。 如果资源管理器的侧边栏已经打开,则单击该图标将导致侧边栏关闭。
-
- Explorer (CTRL+SHIFT+E) – Browse the folder of scripts/files that are you currently working on, referenced as your current workspace. You can create new files, folders, drag and drop files into the folders. 资源管理器(CTRL + SHIFT + E)–浏览当前正在处理的脚本/文件的文件夹,该文件夹/文件被称为当前工作空间。 您可以创建新文件,文件夹,并将文件拖放到文件夹中。
- Search (CTRL+SHIFT+F) – Search the current files you have open in the Editor Groups for specific string. Offers the functionality such as “Find and Replace” and to find all occurrences of the given text. 搜索(CTRL + SHIFT + F)–在编辑器组中打开的当前文件中搜索特定的字符串。 提供“查找和替换”之类的功能,并查找所有出现的给定文本。
- Git (CTRL+SHIFT+G) – Integration with Git, if it is installed on the local machine. Allows you to initialize the current folder/workspace as a Git repository, if not already. Once initialized or if you open a repository folder it offers various functionality to perform against that repository (sync, publish, commit, etc.). Git(CTRL + SHIFT + G)–与Git集成(如果已安装在本地计算机上)。 允许您将当前文件夹/工作空间初始化为Git存储库(如果尚未初始化)。 初始化后,或者如果您打开存储库文件夹,它将提供针对该存储库执行的各种功能(同步,发布,提交等)。
- Debug (CTR+SHIFT+D) – Debug bar utilized for debugging scripts based on support for the language/file extension if supported. 调试(CTR + SHIFT + D)–调试栏,用于基于对语言/文件扩展名的支持来调试脚本。
- Extensions (CTRL+SHIFT+X) – Manage the current extensions installed and search the marketplace for new ones to install. 扩展(CTRL + SHIFT + X)–管理当前安装的扩展,并在市场上搜索要安装的新扩展。
-
- One keyboard shortcut that is nice to use when you are dealing with a large workspace is CTRL+P. When you use this keystroke, you can do an equivalent search of the folder for files by their names. As you type the name in it will bring up the files that match and you can select to open. 当您处理较大的工作空间时,一种很好用的键盘快捷键是CTRL + P。 使用此击键时,可以按名称在文件夹中进行等效搜索。 在其中键入名称时,将弹出匹配的文件,您可以选择打开。
-
- Left side – Most of the functionality added with extensions is found on this side. If you use Git integration you will see indicators such as what branch you have checked out. When you are working with PowerShell files, PSScriptAnalzyer is integrated in the extension and you will see any rules that show warnings or failures. 左侧–扩展中添加的大多数功能都在这一侧。 如果您使用Git集成,则会看到诸如已检出哪个分支的指示符。 当您使用PowerShell文件时,扩展中将集成PSScriptAnalzyer,您将看到显示警告或失败的所有规则。
- Right side – Generally specific to Code functionality, apart from a few that I have worked with (e.g. mssql). You will also see the language that is detected by Code (based on the file extension mapping). Then the smiley face (that is found in most MS products now a days) is used to provide feedback via Twitter. This also contains shortcuts to open an issue with Microsoft via the vscode repository on GitHub. 右侧–除了我曾经使用过的一些功能(例如mssql)以外,通常特定于代码功能。 您还将看到Code检测到的语言(基于文件扩展名映射)。 然后,笑脸(如今已经在大多数MS产品中发现了)用于通过Twitter提供反馈。 它还包含通过GitHub上的vscode存储库与Microsoft共同解决问题的快捷方式。
命令面板 (Command Palette)
This is the bread and butter (to me) of Code. If you learn how to use this fully, you may find yourself never having to touch the mouse or touchpad on your computer again. The keystroke to bring this up is CTRL+SHIFT+P as shown below:
(对我而言)这是代码的基础。 如果您了解如何充分使用它,则可能会发现自己不必再触摸计算机上的鼠标或触摸板。 进行此操作的按键是CTRL + SHIFT + P,如下所示:
One thing I will point out is you can see this starts you with the “>” symbol. If you have used the CTRL+P shortcut you saw that it does not include that symbol, however, if you want to just press CTRL+P and then type the “>” symbol you get the same result. The options you have in the palette are based on the extensions you have installed. As Code grows in functionality so will the built-in or native commands available to you. The extensions that offer commands will generally offer that information in the manual page (just click on an extension in Code to see this).
我要指出的一件事是,您可以看到它以“>”符号开始。 如果您使用了CTRL + P快捷键,则会看到它不包含该符号,但是,如果您只想按CTRL + P,然后键入“>”符号,则会得到相同的结果。 调色板中的选项基于已安装的扩展。 随着Code功能的增加,内置或本机命令也将可用。 提供命令的扩展通常会在手册页中提供该信息(只需单击Code中的扩展即可看到此信息)。
用户设置与工作区设置 (User Settings vs Workspace Settings)
These two items are settings you will find yourself using at various times. The setting files in Code use a JSON format file. These files are created the first time you set anything, whether it be for user or workspace. There is no shortcut keystroke at the time of writing, but you will find both settings under File > Preferences.
这两项是您会在不同时间使用的设置。 代码中的设置文件使用JSON格式文件。 这些文件是在您首次设置任何内容(无论是针对用户还是针对工作空间)时创建的。 在编写本文时,没有快捷键,但是您可以在“文件”>“首选项”下找到这两个设置。
用户 (User)
The user settings are configurations that you set and forget, and this applies to all of Code no matter what project or file you are working on. This includes things like font size or if you want to auto save files you are working on. There are various extensions that will utilize your user settings as well. Each time you open the user settings you will get a split view:
用户设置是您设置并忘记的配置,无论您使用什么项目或文件,这都适用于所有Code。 这包括诸如字体大小之类的内容,或者如果您要自动保存正在处理的文件。 还有各种扩展程序,它们也将利用您的用户设置。 每次打开用户设置时,都会显示一个分割视图:
- the left side includes ALL the settings possible and their default values 左侧包括所有可能的设置及其默认值
- the right side is the current settings you have set for Code. 右侧是您为Code设置的当前设置。
Going through the left side can be a learning point to find things you might want to turn off, or turn on. As you add extensions the left side will include any setting those extensions offer as well. To just note as well, this file is saved in your user profile under “%APPDATA%\Code\User\settings.json”.
穿过左侧可能是一个学习点,可以找到您可能要关闭或打开的东西。 添加扩展时,左侧还将包括这些扩展提供的所有设置。 同样要注意的是,此文件保存在用户配置文件中的“%APPDATA%\ Code \ User \ settings.json”下。
工作空间 (Workspace)
Workspace is like the user settings, with the exception it will only apply to the workspace or folder you currently have open. If you noticed when you opened your first folder it will add a folder called, “.vscode”.
工作区类似于用户设置,但它仅适用于您当前打开的工作区或文件夹。 如果您在打开第一个文件夹时注意到它,则会添加一个名为“ .vscode”的文件夹。
There is nothing in this folder, until you go to File > Preferences > Workspace Settings. You will then see a “settings.json” file is created. This is where you can manipulate things around the current project you are working on. Think of these like environment settings, where you can adjust your environment based on what you are working on.
在您转到文件>首选项>工作区设置之前,此文件夹中没有任何内容。 然后,您将看到一个“ settings.json”文件已创建。 在这里,您可以围绕当前项目进行操作。 考虑这些类似的环境设置,您可以在其中根据自己的工作来调整环境。
扩展名 (Extensions)
While native features in Code are growing, they do not support things like code syntax highlighting or native intellisense for a language. This is where extensions come in to play and help expand what you can do with Code. I am going to walk through the main extensions I use for coding various projects as a Database Administrator.
尽管Code的本机功能正在增长,但它们不支持诸如代码语法突出显示或语言的本机智能感知之类的功能。 这是扩展程序发挥作用的地方,有助于扩展您可以使用Code进行的操作。 我将逐步介绍我用于将各种项目编码为数据库管理员的主要扩展。
电源外壳 (PowerShell)
The PowerShell extension is the only thing you need to develop with PowerShell. It supports using PowerShell version 3.0 or higher, on Windows 7 and higher. It will also support use of PowerShell version 6.0 on Linux and OS X. To run and debug your PowerShell code you will need to adjust your user settings, and just add one item: “powershell.developer.powerShellExePath”. You set this to the PowerShell.exe path on your machine and this executable will be what Code and the debugging services utilize.
PowerShell扩展是使用PowerShell开发所需的唯一内容。 它支持在Windows 7和更高版本上使用PowerShell 3.0或更高版本。 它还将支持在Linux和OS X上使用PowerShell版本6.0。要运行和调试PowerShell代码,您需要调整用户设置,只需添加一项:“ powershell.developer.powerShellExePath”。 您将此设置为计算机上的PowerShell.exe路径,此可执行文件将成为代码和调试服务使用的文件。
调试 (Debugging)
Each language is going to have a configuration to setup when you want to debug a script, that configuration is saved within a “launch.json” file and saved in the “.vscode” folder of your workspace.
当您要调试脚本时,每种语言都将具有要配置的配置,该配置将保存在“ launch.json”文件中,并保存在工作区的“ .vscode”文件夹中。
mssql (mssql)
This is an extension released by Microsoft and supports connecting to Microsoft SQL Server, Azure SQL and SQL Data Warehouse. It is an extension that works with connection profiles, and these profiles are kept within your user settings. If you want to execute a SQL script you just use CTRL+SHIFT+E, and will be prompted for which profile you want to use. An example of the profiles I have configured on my machine:
这是Microsoft发布的扩展 ,支持连接到Microsoft SQL Server,Azure SQL和SQL Data Warehouse。 它是与连接配置文件一起使用的扩展,这些配置文件保留在用户设置内。 如果要执行SQL脚本,只需使用CTRL + SHIFT + E,然后会提示您要使用哪个配置文件。 我在计算机上配置的配置文件示例:
You can see I have profiles for 3 local instances and then one Azure SQL database. The prompts are straightforward to follow for creating a new connection profile. You will also find the template for creating a profile under the user settings, under “MSSQL configuration”. As an example, the profile for “MSSQLSERVER” shown above looks like this in my user settings:
您可以看到我有3个本地实例的配置文件,然后有一个Azure SQL数据库。 提示很容易遵循以创建新的连接配置文件。 您还可以在“ MSSQL配置”下的用户设置下找到用于创建配置文件的模板。 例如,在我的用户设置中,上面显示的“ MSSQLSERVER”配置文件如下所示:
{
"server": "MANATARMS",
"database": "master",
"authenticationType": "Integrated",
"profileName": "MSSQLSERVER",
"password": ""
}
If I wanted to change this to a SQL Login, and have it save the password for me I just add the “user” and “savePassword”:
如果我想将其更改为SQL Login,并让它为我保存密码,我只需添加“用户”和“ savePassword”:
{
"server": "MANATARMS",
"database": "master",
"authenticationType": "SqlLogin",
"profileName": "MSSQLSERVER",
"user": "TestLogin",
"password": ""
"savePassword": true
}
Executing a query will give you a results tab in a separate editor group (there is a message pane in this as well, just not shown in the screenshot):
执行查询将为您提供一个单独的编辑器组中的结果选项卡(此对话框中也有一个消息窗格,只是未在屏幕截图中显示):
Once you are connected you will see the status bar change on the bottom right:
建立连接后,您将在右下方看到状态栏更改:
This shows you the server (or instance) you are connected to, database, and then the login. Anytime you execute a query it will use this connection. As well, any additional SQL file you open within the same workspace will use this connection. If you click on this area of the status bar, it will give you a prompt to change to a different database, if the login has permission. To disconnect you can simply use CTRL+SHIFT+D, or you go through the command palette (just start typing mssql).
这将显示您连接到的服务器(或实例),数据库以及登录名。 每当执行查询时,它将使用此连接。 同样,您在同一工作空间中打开的任何其他SQL文件都将使用此连接。 如果您单击状态栏的此区域,则在登录名允许的情况下,将提示您更改为其他数据库。 要断开连接,您可以简单地使用CTRL + SHIFT + D,或者浏览命令面板(只需开始键入mssql)。
摘要 (Summary)
As database administrator’s role grow and expand into other areas such as Azure or AWS services, you will find Code to be an invaluable tool. I have started using Code as replacement for Notepad++, which I previously used to get syntax highlighting for editing other languages like XML or JSON. Now I have one software tool to use for looking at multiple language files, and can debug them as well. I hope this article helps you get started with Code, and if you have any questions just let me know. You can also find help on Stack Overflow or the GitHub repository for Code. Each extension mentioned above also has a repository on GitHub that you can submit issues to for help and finding bugs.
随着数据库管理员的角色不断壮大并扩展到其他领域,例如Azure或AWS服务,您将发现Code是无价的工具。 我已经开始使用Code代替Notepad ++,之前我曾用它来突出显示语法,以编辑XML或JSON等其他语言。 现在,我有一个软件工具可用于查看多种语言文件,并且还可以调试它们。 希望本文能帮助您开始使用Code,如果有任何疑问,请告诉我。 您还可以在Stack Overflow或GitHub代码仓库上找到帮助。 上面提到的每个扩展都在GitHub上有一个存储库,您可以提交问题以寻求帮助和发现错误。
翻译自: https://www.sqlshack.com/introduction-visual-studio-code-dbas/