您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.
快速入门:使用 .NET Core (C#) 查询数据库Quickstart: Use .NET Core (C#) to query a database
05/29/2020
本文内容
适用于:
Azure SQL 数据库
Azure SQL 托管实例
Azure Synapse Analytics
在本快速入门中,将使用 .NET Core 和 C# 代码连接到数据库。In this quickstart, you'll use .NET Core and C# code to connect to a database. 然后,将运行 Transact-SQL 语句来查询数据。You'll then run a Transact-SQL statement to query data.
先决条件Prerequisites
若要完成本快速入门,你需要:To complete this quickstart, you need:
创建新的 .NET Core 项目Create a new .NET Core project
打开命令提示符,然后创建一个名为 sqltest 的文件夹。Open a command prompt and create a folder named sqltest. 导航到此文件夹,并运行以下命令。Navigate to this folder and run this command.
dotnet new console
此命令将创建新的应用项目文件,包括初始 C# 代码文件 (Program.cs)、XML 配置文件 (sqltest.csproj) 和所需的二进制文件。This command creates new app project files, including an initial C# code file (Program.cs), an XML configuration file (sqltest.csproj), and needed binaries.
在文本编辑器中,打开 sqltest.csproj 并在 标记之间粘贴以下 XML。In a text editor, open sqltest.csproj and paste the following XML between the tags. 此 XML 会添加 System.Data.SqlClient 作为依赖项。This XML adds System.Data.SqlClient as a dependency.
插入代码以查询 Azure SQL 数据库中的数据库Insert code to query the database in Azure SQL Database
在文本编辑器中打开 Program.cs 文件。In a text editor, open Program.cs.
将内容替换为以下代码,为服务器、数据库、用户名和密码添加相应的值。Replace the contents with the following code and add the appropriate values for your server, database, username, and password.
备注
若要使用 ADO.NET 连接字符串,请将代码中设置服务器、数据库、用户名和密码的 4 行替换为以下行。To use an ADO.NET connection string, replace the 4 lines in the code setting the server, database, username, and password with the line below. 在字符串中,设置用户名和密码。In the string, set your username and password.
builder.ConnectionString="";
using System;
using System.Data.SqlClient;
using System.Text;
namespace sqltest
{
class Program
{
static void Main(string[] args)
{
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "";
builder.UserID = "";
builder.Password = "";
builder.InitialCatalog = "";
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
Console.WriteLine("\nQuery data example:");
Console.WriteLine("=========================================\n");
connection.Open();
String sql = "SELECT name, collation_name FROM sys.databases";
using (SqlCommand command = new SqlCommand(sql, connection))
{
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.WriteLine("\nDone. Press enter.");
Console.ReadLine();
}
}
}
运行代码Run the code
在提示符处,运行以下命令。At the prompt, run the following commands.
dotnet restore
dotnet run
验证是否返回了行。Verify that the rows are returned.
Query data example:
=========================================
master SQL_Latin1_General_CP1_CI_AS
tempdb SQL_Latin1_General_CP1_CI_AS
WideWorldImporters Latin1_General_100_CI_AS
Done. Press enter.
选择 Enter 关闭应用程序窗口。Choose Enter to close the application window.
后续步骤Next steps