快速入门:使用 Visual Studio 中的 .NET 和 C# 来连接和查询数据库Quickstart: Use .NET and C# in Visual Studio to connect to and query a database
Azure SQL 数据库
Azure SQL 托管实例
Azure Synapse Analytics
本快速入门展示了如何使用 Visual Studio 中的 .NET Framework 和 C# 代码通过 Transact-SQL 语句查询 Azure SQL 或 Synapse SQL 中的数据库。This quickstart shows how to use the .NET Framework and C# code in Visual Studio to query a database in Azure SQL or Synapse SQL with Transact-SQL statements.
先决条件Prerequisites
若要完成本快速入门,你需要:To complete this quickstart, you need:
具有活动订阅的 Azure 帐户。An Azure account with an active subscription. 创建试用版订阅。Create a Trial Subscription.
Visual Studio 2019 Community、Professional 或 Enterprise 版本。Visual Studio 2019 Community, Professional, or Enterprise edition.
可在其中运行查询的数据库。A database where you can run a query.
可以根据下述快速入门之一,创建数据库,然后对其进行配置:You can use one of these quickstarts to create and then configure a database:
操作Action SQL 数据库SQL Database SQL 托管实例SQL Managed Instance Azure VM 上的 SQL ServerSQL Server on Azure VM 创建Create PortalPortal PortalPortal PortalPortal CLICLI PowerShellPowerShell PowerShellPowerShell PowerShellPowerShell 配置Configure 服务器级别 IP 防火墙规则Server-level IP firewall rule 从 VM 进行连接Connectivity from a VM 来自本地的连接Connectivity from on-premises 获取连接信息Get connection information Azure SQLAzure SQL Azure SQLAzure SQL SQL VMSQL VM
创建用于查询 Azure SQL 数据库中的数据库的代码Create code to query the database in Azure SQL Database
在 Visual Studio 中,创建新的项目。In Visual Studio, create a new project.
在“新建项目”对话框中,选择“Visual C#”,然后选择“控制台应用(.NET Framework)” 。In the New Project dialog, select the Visual C#, Console App (.NET Framework).
输入“sqltest”作为项目名称,然后选择“确定”。Enter sqltest for the project name, and then select OK. 创建新项目。The new project is created.
选择“项目” > “管理 NuGet 包” 。Select Project > Manage NuGet Packages.
在“NuGet 包管理器”中,选择“浏览”选项卡,然后搜索并选择“Microsoft.Data.SqlClient” 。In NuGet Package Manager, select the Browse tab, then search for and select Microsoft.Data.SqlClient.
在“Microsoft.Data.SqlClient”页上选择“安装” 。On the Microsoft.Data.SqlClient page, select Install.
- 如果出现提示,请选择“确定”继续安装。If prompted, select OK to continue with the installation.
- 如果显示“接受许可证”窗口,则选择“我接受” 。If a License Acceptance window appears, select I Accept.
安装完成后,可以关闭“NuGet 包管理器”。When the install completes, you can close NuGet Package Manager.
在代码编辑器中,将 Program.cs 内容替换为以下代码。In the code editor, replace the Program.cs contents with the following code. 替换
<your_server>
、<your_username>
、<your_password>
和<your_database>
的值。Replace your values for<your_server>
,<your_username>
,<your_password>
, and<your_database>
.using System; using Microsoft.Data.SqlClient; using System.Text; namespace sqltest { class Program { static void Main(string[] args) { try { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "<your_server>.database.chinacloudapi.cn"; builder.UserID = "<your_username>"; builder.Password = "<your_password>"; builder.InitialCatalog = "<your_database>"; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { Console.WriteLine("\nQuery data example:"); Console.WriteLine("=========================================\n"); String sql = "SELECT name, collation_name FROM sys.databases"; using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1)); } } } } } catch (SqlException e) { Console.WriteLine(e.ToString()); } Console.ReadLine(); } } }
运行代码Run the code
- 若要运行该应用,请选择“调试” > “开始调试”,或选择工具栏上的“开始”,或按 F5 。To run the app, select Debug > Start Debugging, or select Start on the toolbar, or press F5.
- 验证是否返回了数据库名称和排序规则,然后关闭应用窗口。Verify that the database names and collations are returned, and then close the app window.
后续步骤Next steps
- 了解如何在 Windows/Linux/macOS 中使用 .NET Core 连接和查询 Azure SQL 数据库中的数据库。Learn how to connect and query a database in Azure SQL Database by using .NET Core on Windows/Linux/macOS.
- 了解在 Windows/Linux/macOS 中通过命令行使用 .NET Core 入门。Learn about Getting started with .NET Core on Windows/Linux/macOS using the command line.
- 了解如何使用 SSMS 在 Azure SQL 数据库中设计你的第一个数据库,或者如何使用 .NET 在 Azure SQL 数据库中设计你的第一个数据库。Learn how to Design your first database in Azure SQL Database by using SSMS or Design your first database in Azure SQL Database by using .NET.
- 有关 .NET 的详细信息,请参阅 .NET 文档。For more information about .NET, see .NET documentation.
- 重试逻辑示例:使用 ADO.NET 弹性连接到 Azure SQL。Retry logic example: Connect resiliently to Azure SQL with ADO.NET.