快速入门:使用 Azure Data StudioAzure Data Studio 连接并查询 SQL ServerQuickstart: Connect and query SQL Server using Azure Data StudioAzure Data Studio

本快速入门介绍如何使用 Azure Data StudioAzure Data Studio 连接到 SQL Server,然后使用 Transact-SQL (T-SQL) 语句创建 Azure Data StudioAzure Data Studio 教程中使用的 TutorialDB 。This quickstart shows how to use Azure Data StudioAzure Data Studio to connect to SQL Server, and then use Transact-SQL (T-SQL) statements to create the TutorialDB used in Azure Data StudioAzure Data Studio tutorials.

必备条件Prerequisites

要完成本快速入门,需要 Azure Data StudioAzure Data Studio,并访问 SQL Server。To complete this quickstart, you need Azure Data StudioAzure Data Studio, and access to SQL Server.

如果没有 SQL Server 的访问权限,请从以下链接选择你的平台(请确保记住 SQL 登录名和密码!):If you don't have access to a SQL Server, select your platform from the following links (make sure you remember your SQL Login and Password!):

连接到 SQL ServerConnect to a SQL Server

  1. 启动 Azure Data StudioAzure Data StudioStart Azure Data StudioAzure Data Studio.

  2. 首次运行 Azure Data StudioAzure Data Studio 时,应该会打开“欢迎”页 。The first time you run Azure Data StudioAzure Data Studio the Welcome page should open. 如果没有看到“欢迎”页,请选择“帮助” > “欢迎” 。If you don't see the Welcome page, select Help > Welcome. 选择“新建连接”以打开“连接”窗格 :Select New Connection to open the Connection pane:

    新建连接图标

  3. 本文使用 SQL 登录名,但也支持 Windows 身份验证 。This article uses SQL Login, but Windows Authentication is supported. 按如下所示填写字段:Fill in the fields as follows:

  • 服务器名称: - 在此处输入服务器名称。Server Name: - Enter server name here. 例如,localhost。For example, localhost.

  • 身份验证类型: SQL 登录名Authentication Type: SQL Login

  • 用户名: SQL Server 的用户名User name: User name for the SQL Server

  • 密码: SQL Server 的密码Password: Password for the SQL Server

  • 数据库名称: 此字段不填Database Name: leave this field blank

  • 服务器组: <默认>Server Group: <Default>

    新建连接屏幕

创建数据库Create a database

以下步骤创建一个名为“TutorialDB”的数据库 :The following steps create a database named TutorialDB:

  1. 右键单击服务器“localhost”,然后选择“新建查询” 。Right-click on your server, localhost, and select New Query.

  2. 将以下代码片段粘贴到查询窗口,然后选择“运行” 。Paste the following snippet into the query window: and then select Run.

USE master
GO
IF NOT EXISTS (
 SELECT name
 FROM sys.databases
 WHERE name = N'TutorialDB'
)
 CREATE DATABASE [TutorialDB];
GO
IF SERVERPROPERTY('ProductVersion') > '12'
 ALTER DATABASE [TutorialDB] SET QUERY_STORE=ON;
GO

完成查询后,新的“TutorialDB”会显示在数据库列表中 。After the query completes, the new TutorialDB appears in the list of databases. 如果未显示,请右键单击“数据库”节点,然后选择“刷新” 。If you don't see it, right-click the Databases node and select Refresh.

创建数据库

创建表Create a table

查询编辑器仍连接到 master 数据库,但需要在 TutorialDB 数据库中创建一个表 。The query editor is still connected to the master database, but we want to create a table in the TutorialDB database.

  1. 将连接上下文更改为 TutorialDB :Change the connection context to TutorialDB:

    更改上下文

  2. 将以下代码片段粘贴到查询窗口,并单击“运行” :Paste the following snippet into the query window and click Run:

    备注

    还可以在编辑器中追加此片段,或覆盖之前的查询。You can append this too, or overwrite the previous query in the editor. 请注意,单击“运行”仅执行选定的查询 。Note that clicking Run executes only the query that is selected. 如果未选择任何内容,单击“运行”将执行编辑器中所有的查询 。If nothing is selected, clicking Run executes all queries in the editor.

-- Create a new table called 'Customers' in schema 'dbo'
-- Drop the table if it already exists
IF OBJECT_ID('dbo.Customers', 'U') IS NOT NULL
 DROP TABLE dbo.Customers;
GO
-- Create the table in the specified schema
CREATE TABLE dbo.Customers
(
 CustomerId int NOT NULL PRIMARY KEY, -- primary key column
 Name nvarchar(50) NOT NULL,
 Location nvarchar(50) NOT NULL,
 Email nvarchar(50) NOT NULL
);
GO

完成查询后,新的“客户”表会显示在表列表中 。After the query completes, the new Customers table appears in the list of tables. 可能需要右键单击“TutorialDB”>“表”节点,然后选择“刷新” 。You might need to right-click the TutorialDB > Tables node and select Refresh.

插入行Insert rows

  • 将以下代码片段粘贴到查询窗口,并单击“运行” :Paste the following snippet into the query window and click Run:
-- Insert rows into table 'Customers'
INSERT INTO dbo.Customers
 ([CustomerId], [Name], [Location], [Email])
VALUES
 ( 1, N'Orlando', N'Australia', N''),
 ( 2, N'Keith', N'India', N'keith0@adventure-works.com'),
 ( 3, N'Donna', N'Germany', N'donna0@adventure-works.com'),
 ( 4, N'Janet', N'United States', N'janet1@adventure-works.com')
GO

查看查询返回的数据View the data returned by a query

  1. 将以下代码片段粘贴到查询窗口,并单击“运行” :Paste the following snippet into the query window and click Run:
-- Select rows from table 'Customers'
SELECT * FROM dbo.Customers;

选择结果

后续步骤Next steps

现在,已成功连接到 SQL Server 并运行查询,下面请尝试代码编辑器教程Now that you've successfully connected to SQL Server and run a query try out the Code editor tutorial.