快速入门:使用 Ruby 查询 Azure SQL 数据库中的数据库或 Azure SQL 托管实例Quickstart: Use Ruby to query a database in Azure SQL Database or Azure SQL Managed Instance
Azure SQL 数据库
Azure SQL 托管实例
本快速入门演示了如何使用 Ruby 连接到数据库并使用 Transact-SQL 语句查询数据。This quickstart demonstrates how to use Ruby to connect to a database and query data with Transact-SQL statements.
先决条件Prerequisites
若要完成本快速入门,需要具备以下先决条件:To complete this quickstart, you need the following prerequisites:
数据库。A database. 可以使用以下某个快速入门创建数据库,然后对其进行配置:You can use one of these quickstarts to create and then configure the database:
操作Action SQL 数据库SQL Database SQL 托管实例SQL Managed Instance Azure VM 上的 SQL ServerSQL Server on Azure VM 创建Create 门户Portal 门户Portal PortalPortal CLICLI CLICLI PowerShellPowerShell PowerShellPowerShell PowerShellPowerShell 配置Configure 服务器级别 IP 防火墙规则Server-level IP firewall rule 从 VM 进行连接Connectivity from a VM 来自本地的连接Connectivity from on-premises 连接到 SQL Server 实例Connect to a SQL Server instance 加载数据Load data 根据快速入门加载的 Adventure WorksAdventure Works loaded per quickstart 还原 Wide World ImportersRestore Wide World Importers 还原 Wide World ImportersRestore Wide World Importers 从 GitHub 的一个 BACPAC 文件还原或导入 Adventure WorksRestore or import Adventure Works from a BACPAC file from GitHub 从 GitHub 所提供的 BACPAC 文件还原或导入 Adventure WorksRestore or import Adventure Works from a BACPAC file from GitHub 重要
本文中脚本的编写目的是使用 Adventure Works 数据库。The scripts in this article are written to use the Adventure Works database. 使用 SQL 托管实例时,必须将 Adventure Works 数据库导入一个实例数据库,或者修改本文中的脚本,以便使用 Wide World Importers 数据库。With a SQL Managed Instance, you must either import the Adventure Works database into an instance database or modify the scripts in this article to use the Wide World Importers database.
适用于你的操作系统的 Ruby 和相关软件:Ruby and related software for your operating system:
macOS:安装 Homebrew、rbenv 和 ruby-build、Ruby、FreeTDS 和 TinyTDS。macOS: Install Homebrew, rbenv and ruby-build, Ruby, FreeTDS, and TinyTDS. 请参阅 Create Ruby apps using SQL Server on macOS(在 macOS 上使用 SQL Server 创建 Ruby 应用)中的步骤 1.2、1.3、1.4、1.5 和 2.1。See Steps 1.2, 1.3, 1.4, 1.5, and 2.1 in Create Ruby apps using SQL Server on macOS.
Ubuntu:安装 Ruby、rbenv 和 ruby-build、Ruby、FreeTDS 和 TinyTDS 的先决条件。Ubuntu: Install prerequisites for Ruby, rbenv and ruby-build, Ruby, FreeTDS, and TinyTDS. 请参阅 Create Ruby apps using SQL Server on Ubuntu(在 Ubuntu 上使用 SQL Server 创建 Ruby 应用)中的步骤 1.2、1.3、1.4、1.5 和 2.1。See Steps 1.2, 1.3, 1.4, 1.5, and 2.1 in Create Ruby apps using SQL Server on Ubuntu.
Windows:安装 Ruby、Ruby Devkit 和 TinyTDS。Windows: Install Ruby, Ruby Devkit, and TinyTDS. 请参阅配置用于 Ruby 开发的开发环境。See Configure development environment for Ruby development.
获取服务器连接信息Get server connection information
获取连接到 Azure SQL 数据库中的数据库所需的连接信息。Get the connection information you need to connect to a database in Azure SQL Database. 在后续过程中,将需要完全限定的服务器名称或主机名称、数据库名称和登录信息。You'll need the fully qualified server name or host name, database name, and login information for the upcoming procedures.
登录到 Azure 门户。Sign in to the Azure portal.
导航到“SQL 数据库”或“SQL 托管实例”页。Navigate to the SQL databases or SQL Managed Instances page.
在“概述”页上,在“Server 名称”旁查看 Azure SQL 数据库中的数据库的完全限定服务器名称,或在“Host”旁边查看 Azure VM 上的 Azure SQL 托管实例或 SQL Server 的完全限定服务器名称(或 IP 地址) 。On the Overview page, review the fully qualified server name next to Server name for a database in Azure SQL Database or the fully qualified server name (or IP address) next to Host for an Azure SQL Managed Instance or SQL Server on Azure VM. 若要复制服务器名称或主机名称,请将鼠标悬停在其上方,然后选择“复制”图标。To copy the server name or host name, hover over it and select the Copy icon.
备注
有关 Azure VM 上的 SQL Server 的连接信息,请参阅连接到 SQL Server 实例。For connection information for SQL Server on Azure VM, see Connect to a SQL Server instance.
创建用于查询 Azure SQL 数据库中的数据库的代码Create code to query your database in Azure SQL Database
在文本或代码编辑器中,创建新文件 sqltest.py。In a text or code editor, create a new file named sqltest.rb.
添加以下代码。Add the following code. 将 Azure SQL 数据库中的数据库的值替换为
<server>
、<database>
、<username>
和<password>
。Substitute the values from your database in Azure SQL Database for<server>
,<database>
,<username>
, and<password>
.重要
本示例中的代码使用示例 AdventureWorksLT 数据,在创建数据库时可以选择该数据作为源。The code in this example uses the sample AdventureWorksLT data, which you can choose as source when creating your database. 如果数据库有不同数据,请在 SELECT 查询中使用自己数据库中的表。If your database has different data, use tables from your own database in the SELECT query.
require 'tiny_tds' server = '<server>.database.chinacloudapi.cn' database = '<database>' username = '<username>' password = '<password>' client = TinyTds::Client.new username: username, password: password, host: server, port: 1433, database: database, azure: true puts "Reading data from table" tsql = "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid" result = client.execute(tsql) result.each do |row| puts row end
运行代码Run the code
请在命令提示符处运行以下命令:At a command prompt, run the following command:
ruby sqltest.rb
验证是否返回了数据库中的前 20 个类别/产品行。Verify that the top 20 Category/Product rows from your database are returned.