简体   繁体   English

无法在Linux上使用SQLConnection与Azure SQL连接

[英]Can't connect with Azure SQL using SQLConnection on Linux

I'm currently developing an ASP.NET Core WebApi that uses an Azure SQL Db for data persistence. 我目前正在开发使用Azure SQL Db进行数据持久化的ASP.NET Core WebApi。 I use Linux Mint 19 as a development environment. 我使用Linux Mint 19作为开发环境。 Unfortunately when dbConnection.Open() is called I keep getting SQLException: 不幸的是,当调用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)'

Here's my code: 这是我的代码:

 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();

            }
        }

I've tried both using SQLConnectionStringBuilder and passing connectionString with my credentials. 我已经尝试过使用SQLConnectionStringBuilder和使用我的凭据传递connectionString。 None of it worked. 没有一个起作用。

I've opened port 1433 using the command: 我使用以下命令打开了端口1433:

sudo ufw allow 1433

but it didn't help, still getting the same exception. 但这并没有帮助,仍然出现相同的异常。

Any help would be much appreciated. 任何帮助将非常感激。

Thanks. 谢谢。

UPDATE UPDATE

I managed to connect with Azure SQL. 我设法连接到Azure SQL。 It looks like my ISP blocks connection. 看来我的ISP阻止了连接。 I was able to connect using my smartphone WiFi hotspot. 我可以使用智能手机WiFi热点进行连接。

First, make sure you have created appropriate firewall rule to allow traffic from your network to reach Azure SQL Database. 首先,请确保已创建适当的防火墙规则,以允许来自网络的流量到达Azure SQL数据库。 Learn here how to do it. 在此处了解如何操作。

Please try to ping your SQL Azure server. 请尝试ping您的SQL Azure服务器。 The ping should fail but it should return the current IP Address of the Azure SQL Database logical server. ping应失败,但应返回Azure SQL数据库逻辑服务器的当前IP地址。 If you cannot see that IP returned then the issue is a DNS issue. 如果看不到返回的IP,则问题是DNS问题。

With the IP address returned on the previous step, try to telnet that IP address on port 1433. 在上一步返回IP地址后,尝试通过端口1433远程登录该IP地址。

telnet 181.37.11.112 1433 telnet 181.37.11.112 1433

If you cannot connect using telnet, use traceroute command to diagnose when locate where data loss occurs. 如果无法使用telnet连接,请使用traceroute命令诊断何时定位发生数据丢失的位置。

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

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