简体   繁体   English

来自 C# 的 Azure SQL DB 连接问题

[英]Azure SQL DB connection issues from C#

I'm having trouble connecting with my .NET 5 web app to an Azure SQL DB.我在将 .NET 5 Web 应用程序连接到 Azure SQL DB 时遇到问题。 The weird thing is that I can connect to the DB using Azure Data Studio.奇怪的是我可以使用 Azure Data Studio 连接到数据库。

I've configured the Azure SQL Server firewall to accept connections from my IP address, and all of the Inbound/Outbound IP addresses of my Azure App Service.我已将 Azure SQL Server 防火墙配置为接受来自我的 IP 地址以及 Azure 应用服务的所有入站/出站 IP 地址的连接。 Before adding my local IP address to the firewall, I could not connect with Azure Data Studio, so that works as intended, but then I use the connection string provided by Azure itself (in ADO.NET format, since I'm using Entity Framework) inside my web app, and the app can't connect to the DB (it times out with 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: TCP Provider, error: 40 - Could not open a connection to SQL Server) ).在将本地 IP 地址添加到防火墙之前,我无法与 Azure Data Studio 连接,因此按预期工作,但随后我使用了 Azure 本身提供的连接字符串(采用 ADO.NET 格式,因为我使用的是实体框架) ) 在我的 Web 应用程序中,并且该应用程序无法连接到数据库(在与SQL Server 建立连接时发生网络相关或特定于实例的错误而超时。找不到服务器或无法访问服务器。验证实例名称正确并且 SQL Server 配置为允许远程连接。(提供程序:TCP 提供程序,错误:40 - 无法打开到 SQL Server 的连接) )。

I can't really understand why my localhost instance can't connect, as the IP address trying to connect to the db is the same as when I use Azure Data Studio!我真的不明白为什么我的 localhost 实例无法连接,因为尝试连接到数据库的 IP 地址与我使用 Azure Data Studio 时的 IP 地址相同! And also the App Service instance in Azure can't connect either (in the SQL Server's firewall I've also enabled connectivity from Azure App Services!!).而且 Azure 中的应用服务实例也无法连接(在 SQL Server 的防火墙中,我还启用了来自 Azure 应用服务的连接!!)。 The connection string is of the form连接字符串的格式为

Server=tcp:[my server].database.windows.net;Initial Catalog=[my db];Persist Security Info=False;User ID=[my username];Password=[my password];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30; Server=tcp:[my server].database.windows.net;Initial Catalog=[my db];Persist Security Info=False;User ID=[my username];Password=[my password];MultipleActiveResultSets=False;Encrypt= True;TrustServerCertificate=False;连接超时=30;

Any hints?任何提示?

EDIT: to recap as requested,编辑:按要求回顾,

  • dotnet run locally doesn't connect dotnet 在本地运行无法连接
  • containerized app service doesn't connect容器化应用服务无法连接
  • Azure Data Studio locally does connect Azure Data Studio 本地连接

I've found out the issue in the meantime, but I don't understand the cause:我在此期间发现了问题,但我不明白原因:

  • dotnet run from WSL doesn't work;从 WSL 运行的 dotnet 不起作用; if I run the app from Windows it does work!!如果我从 Windows 运行该应用程序,它确实可以工作!!
  • I've therefore changed the App Service to build from source instead of running a containerized image, and it also does work.因此,我将应用服务更改为从源代码构建,而不是运行容器化映像,而且它也确实有效。

Why would running the app locally from WSL instead of Windows result in the Azure SQL Server's firewall blocking me?!为什么从 WSL 而不是 Windows 本地运行应用程序会导致 Azure SQL Server 的防火墙阻止我?! Is WSL exposed to the internet with a different IP address? WSL 是否使用不同的 IP 地址暴露给 Internet?

And why does the same happen to an Azure App Service that runs the containerized version of my app?为什么运行我的应用程序容器化版本的 Azure 应用服务会发生同样的情况?

Since you can access the server from the same machine, it is not a firewall issue.由于您可以从同一台计算机访问服务器,因此这不是防火墙问题。

There are two possibilities you could check:您可以检查两种可能性:

  1. The connection string that your program is using is not what you expect.您的程序使用的连接字符串不是您所期望的。 Try logging the connection string.尝试记录连接字符串。
  2. The server is configured to use named pipes and not tcp.服务器配置为使用命名管道而不是 tcp。 Azure Data Studio is configured to use named pipes and therefore works. Azure Data Studio 配置为使用命名管道,因此可以正常工作。 Try checking the server configuration.尝试检查服务器配置。

error: 40 - Could not open a connection to SQL Server).错误:40 - 无法打开与 SQL Server 的连接)。

  • In most cases, this error occurs because the SQL Server service is stopped.在大多数情况下,发生此错误是因为 SQL Server 服务已停止。
  • If you are using SQL Server Express with Advanced Services or a named instance, this error will occur if the report server URL or connection string for the report server database is not correct.如果您将 SQL Server Express 与高级服务或命名实例一起使用,并且报表服务器数据库的报表服务器 URL 或连接字符串不正确,则会发生此错误。

To work through these issues:要解决这些问题:

  • Verify that the SQL Server ( MSSQLSERVER ) service is started.验证 SQL Server ( MSSQLSERVER ) 服务是否已启动。
  • Make sure that TCP/IP is enabled as a client protocol on the application server.确保在应用程序服务器上启用 TCP/IP 作为客户端协议。
  • Try increasing the connection timeout value.尝试增加连接超时值。 We recommend using a connection timeout of at least 30 seconds.我们建议使用至少 30 秒的连接超时。
  • Search for services in control panel, that will show a window with all services in your system, find your service SQLEXPRESS and click on Start .在控制面板中搜索服务,这将显示一个包含系统中所有服务的窗口,找到您的服务SQLEXPRESS并单击开始 It should start your server instance.它应该启动您的服务器实例。
  • If it is already in Start.如果它已经在开始。 Just Stop and start or simply Restart it.只需停止启动或简单地重新启动它。

在此处输入图片说明

Go to SQL Server Configuration manager --> SQL Server Network Configuration -->Protocol for MSSQLSERVER In the right pane split page you will have to disable转到 SQL Server 配置管理器 --> SQL Server 网络配置 --> MSSQLSERVER 的协议 在右窗格拆分页中,您必须禁用

  • Shared Memory - Enable共享内存 - 启用
  • Named Pipes - Enable命名管道 - 启用
  • TCP/IP - Enable TCP/IP - 启用

Please refer Connectivity Errors , Troubleshoot connecting to the SQL Server , Known Issue and error: 40 and SO请参阅连接错误连接到 SQL Server 的疑难解答已知问题错误:40SO

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

相关问题 C# MVC Core 5 Site 在尝试打开与 Azure SQL DB 的连接时引发编码错误 - C# MVC Core 5 Site throws encoding error when trying to open a connection to Azure SQL DB 来自C#的DB2连接 - DB2 connection from C# 如何使用C#创建与UWP app和Azure DB的基本连接 - How to create basic connection with UWP app and Azure DB with C# 如何最小化来自C#helper类的sql db连接命令的使用语句 - how to minimize using statements on sql db connection commands from C# helper class C# Sql 连接字符串,试图从另一台计算机连接到本地数据库 - C# Sql connection string, trying to connect to local db from another computer 使用 Active Directory 密码从 C# 连接到 Sql Azure Db 时出现问题 - Trouble connecting to Sql Azure Db from C# using Active Directory password C#定时器,以保持SQL服务器连接到数据库“活着” - c# Timer to keep the sql-server connection to db “Alive” Microsoft Azure Sql 从 VS 2019 连接时连接速度很慢 C# - Microsoft Azure Sql connection is slow while connect from VS 2019 C# 使用 c# 连接到 Azure VM 的 SQL DB - Connect to SQL DB of Azure VM using c# C# 上传数据到 Azure Cosmos DB SQL Z8A5DA52ED1260421D359E7AAZ0C - C# to upload data to Azure Cosmos DB SQL api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM