Azure上的.NET Core无法连接到SQL Server数据库

问题描述 投票:1回答:1

我有一个.NET Core API后端,该后端发布在Azure上。我也有一个SQL Server数据库在Azure上运行,如果我在本地运行后端应用程序,它将成功连接到online数据库,读/写等工作正常。但是,当运行在线后端时,每个API调用都会导致500错误。在日志中查看时,可能会导致以下错误500:

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

因此出于某种原因,Azure上托管的后端无权访问此数据库。在我的在线数据库的Azure门户中,我已将后端的IP添加到防火墙例外中,并选中了“允许与服务器的远程Azure连接”。

TLDR;在本地一切正常,在Azure上发布的版本无法连接/查找数据库。

.net sql-server azure core
1个回答
0
投票

UPDATE

我根据教程进行操作,有关详细步骤,请参见屏幕截图。 The official documentation包含示例代码,您可以下载并查看。

步骤1.修改代码中的连接字符串,并添加测试接口。

enter image description here

步骤2.为个人创建Todo表。

enter image description here

步骤3.在本地测试然后部署。您可以看到我的项目成功运行并且可以使用界面。

enter image description here

PRIVIOUS

您可以转到sqldb,找到Connection strings并复制那里提供的那个。

此字符串的格式为:

Server=tcp:servername.database.windows.net,1433;Initial Catalog=db;
Persist Security Info=False;
User ID=user;Password=mypassword;
MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;
Connection Timeout=30;

也有a post具有相同的问题。有关更多详细信息,您可以阅读。

© www.soinside.com 2019 - 2024. All rights reserved.