gpt4 book ai didi

c# - 在 docker 容器中连接到 SQL Server 时出错

转载 作者:行者123 更新时间:2023-11-30 18:12:23 24 4
gpt4 key购买 nike

我有一个在 Windows Docker 容器中运行的 ASP.NET Core WebAPI。我在从应用程序内部连接到本地 SQL 实例时遇到问题。

这是错误信息:

(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) ---> System.ComponentModel.Win32Exception (53): The network path was not found

我找到了 this issue on github ,但它并不真正适用于我的情况。

我可以通过用服务器本地 IP 地址替换实例名称来使一切正常工作,如下所示:

"ConnectionString_ThrowingError": "Data Source=MySqlInstance; (...)"
"ConnectionString_WorkingFine": "Data Source=192.168.0.123; (...)"

这里发生了什么? .NET 似乎无法解析域计算机主机名。


其他详情

  • 使用.Net Core 2.2
  • 在 Windows 上使用 Docker Windows 容器
  • 尝试在 Windows 10Windows Server 2019 上托管。两者都有完全相同的错误。
  • 我可以使用主机名或本地 IP 从容器内部ping SQL 实例。
  • 当我用连接字符串中的本地 IP 地址替换实例名称时,一切正常。
  • 如果我在 .NET CLI(不使用 Docker)中运行应用程序,一切正常
  • 我尝试访问的 SQL 实例未托管在 Docker 上。它托管在同一本地网络上的另一台 Windows 服务器上。
  • 我可以使用 DataSource=MyAzureServer.database.windows.net 访问 Azure 上托管的其他 SQL 实例,没有任何问题。

最佳答案

根据我在评论中的建议,如果服务器名称不是完全限定的域名 (FQDN),则可能无法将服务器解析为 IP。

尝试将服务器名称调整为 FQDN,希望这样可以解决问题。

否则,请查看 this one 等链接从 SQL Server 的角度计算名称解析,您将获得一些关于如何解决此类问题的其他想法。

关于c# - 在 docker 容器中连接到 SQL Server 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55463316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com