简体   繁体   English

Azure 上的 .NET Core 无法连接到 SQL 服务器数据库

[英].NET Core on Azure can't connect to SQL Server Database

I have a .NET Core API backend which is published on Azure.我有一个 .NET 核心 API 后端,它发布在 Azure 上。 I also have a SQL Server database running on Azure, if I run my backend app locally it succesfully connects to the online database, reading/writing etc works fine.我还有一个在 Azure 上运行的 SQL 服务器数据库,如果我在本地运行我的后端应用程序,它会成功连接到在线数据库,读/写等工作正常。 When running the online backend however, every API call results in a 500 error.但是,在运行在线后端时,每个 API 调用都会导致 500 错误。 When looking in the logs there is the following error which probably causes the 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)

So for some reason the backend hosted on Azure does not have access to this database.因此,由于某种原因,托管在 Azure 上的后端无法访问此数据库。 In the Azure portal of my online db I have added the IP of my backend to firewall exception and "Allow remote Azure connections to server" is checked.在我的在线数据库的 Azure 门户中,我已将我的后端的 IP 添加到防火墙异常,并检查了“允许远程 Azure 连接到服务器”。

TLDR; TLDR; everything works fine locally, published version on Azure can't connect to/find database.在本地一切正常,在 Azure 上发布的版本无法连接/查找数据库。

Edit: this is resolved, thanks everyone who commented.编辑:已解决,感谢所有评论的人。 (special thanks to Jason!) Solution was to use the following format of connection string: (特别感谢 Jason!)解决方案是使用以下格式的连接字符串:

Data Source=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;

UPDATE更新

I operate according to the tutorial, see the screenshot for detailed steps.我按照教程操作,详细步骤见截图。 The official documentation contains sample code, which you can download and see. 官方文档包含示例代码,您可以下载查看。 I put my conn string in my code.我将我的 conn 字符串放入我的代码中。

If you configure your connnection strings in web.config or other files, u can see this document .如果您在 web.config 或其他文件中配置连接字符串,您可以查看此文档 And u can configure it in your portal.你可以在你的门户中配置它。

在此处输入图像描述

Step 1. Modify connection strings in code, and add a interface for test. Step 1.修改代码中的连接字符串,添加测试接口。

在此处输入图像描述

Step 2. Create a Todo Table for peoject.步骤 2.为 peoject 创建一个待办事项表。 [Make sure you have finished Firewall setting ] [确保您已完成Firewall setting ]

在此处输入图像描述

Step 3. Test in local then deploy.步骤 3.在本地测试然后部署。 You can see my project run successfully and interface can be used.可以看到我的项目运行成功,界面可以使用了。

在此处输入图像描述

PRIVIOUS私人的

You can go to your sqldb,find Connection strings and copied the one provided there.您可以 go 到您的 sqldb,找到Connection strings并复制那里提供的字符串。

This string was in the format:该字符串的格式为:

Data Source=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;

There also have a post which has the same issue.还有一个帖子也有同样的问题。 For more details,u can read it.有关更多详细信息,您可以阅读它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM