快速入门:使用 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 SQL 数据库中的数据库的代码Create code to query the database in Azure SQL Database

  1. 在 Visual Studio 中,创建新的项目。In Visual Studio, create a new project.

  2. 在“新建项目”对话框中,选择“Visual C#”,然后选择“控制台应用(.NET Framework)” 。In the New Project dialog, select the Visual C#, Console App (.NET Framework).

  3. 输入“sqltest”作为项目名称,然后选择“确定”。Enter sqltest for the project name, and then select OK. 创建新项目。The new project is created.

  4. 选择“项目” > “管理 NuGet 包” 。Select Project > Manage NuGet Packages.

  5. 在“NuGet 包管理器”中,选择“浏览”选项卡,然后搜索并选择“Microsoft.Data.SqlClient” 。In NuGet Package Manager, select the Browse tab, then search for and select Microsoft.Data.SqlClient.

  6. 在“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.
  7. 安装完成后,可以关闭“NuGet 包管理器”。When the install completes, you can close NuGet Package Manager.

  8. 在代码编辑器中,将 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

  1. 若要运行该应用,请选择“调试” > “开始调试”,或选择工具栏上的“开始”,或按 F5 。To run the app, select Debug > Start Debugging, or select Start on the toolbar, or press F5.
  2. 验证是否返回了数据库名称和排序规则,然后关闭应用窗口。Verify that the database names and collations are returned, and then close the app window.

后续步骤Next steps