.net mysql查询数据库连接_使用 .NET Core 连接和查询数据库 - Azure SQL Database & SQL Managed Instance | Microsoft Docs...

这篇快速入门教程介绍了如何使用.NET Core和C#代码连接到Azure SQL数据库,然后运行Transact-SQL语句查询数据。教程中包含了创建.NET Core项目、设置连接字符串以及处理查询结果的步骤。
摘要由CSDN通过智能技术生成

您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用 .NET Core (C#) 查询数据库Quickstart: Use .NET Core (C#) to query a database

05/29/2020

本文内容

适用于:

5efeb2c6b4f69438343eaea1a6922fe0.png

Azure SQL 数据库

5efeb2c6b4f69438343eaea1a6922fe0.png

Azure SQL 托管实例

5efeb2c6b4f69438343eaea1a6922fe0.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值