gpt4 book ai didi

c# - 无法在 Linux 上使用 SQLConnection 连接 Azure SQL

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:55 25 4
gpt4 key购买 nike

我目前正在开发一个 ASP.NET Core WebApi,它使用 Azure SQL Db 来实现数据持久性。我使用 Linux Mint 19 作为开发环境。不幸的是,当调用 dbConnection.Open() 时,我不断收到 SQLException:

Exception has occurred: CLR/System.Data.SqlClient.SqlException
An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Data.SqlClient.dll but was not handled in user code: '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)'

这是我的代码:

 private const string connectionString = "Server=tcp:{myserver}.database.windows.net,1433;Initial Catalog={mydb};Persist Security Info=False;User ID={user};Password={pass};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "my_server.database.windows.net";
builder.UserID = "my_user";
builder.Password = "my_pass";
builder.InitialCatalog = "my_db";

using(IDbConnection dbConnection = new SqlConnection(builder.ConnectionString)){
dbConnection.Open();
var result = dbConnection.Query<Task>("SELECT * FROM Tasks");

if (dbConnection.State == ConnectionState.Open)
{
dbConnection.Close();

}
}

我尝试过使用 SQLConnectionStringBuilder 并使用我的凭据传递 connectionString。这些都不起作用。

我已经使用命令打开了端口 1433:

sudo ufw allow 1433

但这并没有帮助,仍然得到同样的异常。

如有任何帮助,我们将不胜感激。

谢谢。

更新

我设法连接到 Azure SQL。看起来我的 ISP 阻止了连接。我能够使用我的智能手机 WiFi 热点进行连接。

最佳答案

首先,确保您已创建适当的防火墙规则以允许来自网络的流量到达 Azure SQL 数据库。学习here怎么做。

请尝试 ping 您的 SQL Azure 服务器。 ping 应该失败,但它应该返回 Azure SQL 数据库逻辑服务器的当前 IP 地址。如果您看不到返回的 IP,则问题是 DNS 问题。

使用上一步返回的 IP 地址,尝试在端口 1433 上远程登录该 IP 地址。

远程登录 181.37.11.112 1433

如果使用telnet无法连接,请使用traceroute命令进行诊断,定位发生数据丢失的位置。

关于c# - 无法在 Linux 上使用 SQLConnection 连接 Azure SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52506542/

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