簡體   English   中英

無法在Linux上使用SQLConnection與Azure SQL連接

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

我目前正在開發使用Azure SQL Db進行數據持久化的ASP.NET Core WebApi。 我使用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

但這並沒有幫助,仍然出現相同的異常。

任何幫助將非常感激。

謝謝。

UPDATE

我設法連接到Azure SQL。 看來我的ISP阻止了連接。 我可以使用智能手機WiFi熱點進行連接。

首先,請確保已創建適當的防火牆規則,以允許來自網絡的流量到達Azure SQL數據庫。 在此處了解如何操作。

請嘗試ping您的SQL Azure服務器。 ping應失敗,但應返回Azure SQL數據庫邏輯服務器的當前IP地址。 如果看不到返回的IP,則問題是DNS問題。

在上一步返回IP地址后,嘗試通過端口1433遠程登錄該IP地址。

telnet 181.37.11.112 1433

如果無法使用telnet連接,請使用traceroute命令診斷何時定位發生數據丟失的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM