7

我有一個SQL Azure實例設置,並可以連接到它沒有來自SQL Server Management Studio的問題。然而,當我的應用程序試圖連接,出現此錯誤:無法連接到SQL Azure從應用程序,但可以從SSMS

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

我使用實體框架代碼第一次4.1和ASP.NET MVC 3,我的應用程序成功地利用了SQL Express最初被開發。現在我正在使用this tutorial to move the database to SQL Azure(應用程序最終會移動到那裏,但開發仍在繼續)。

由於SSMS工作正常,我猜它歸結爲web.config?我試着連接字符串名稱的每個組合:

<connectionStrings> 
    <add name="ApplicationServices" connectionString="Server=tcp:suppressed.database.windows.net,1433;Database=EventsTest;User ID=suppressed;Password=suppressed;Trusted_Connection=False;Encrypt=True;PersistSecurityInfo=True;" providerName="System.Data.SqlClient" /> 
    <add name="DomainContext" connectionString="Server=tcp:suppressed.database.windows.net,1433;Database=EventsTest;User ID=suppressed;Password=suppressed;Trusted_Connection=False;Encrypt=True;PersistSecurityInfo=True;" providerName="System.Data.SqlClient" /> 
    <add name="Events.DataAccess.EntityFramework.DomainContext" connectionString="Server=tcp:suppressed.database.windows.net,1433;Database=EventsTest;User ID=suppressed;Password=suppressed;Trusted_Connection=False;Encrypt=True;PersistSecurityInfo=True;" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

我還試圖Wireshark的,我不是很瞭解,但似乎暗示了一些活動(192.168.1.101是我的機器,207.46.63.13是SQL Azure服務器):

 
1 0.000000 192.168.1.101 207.46.63.13 TCP [TCP segment of a reassembled PDU] 
2 0.116269 207.46.63.13 192.168.1.101 TCP ms-sql-s > bmc-net-adm [ACK] Seq=1 Ack=2 Win=8444 Len=0 
3 2.091928 192.168.1.101 207.46.63.13 TCP [TCP segment of a reassembled PDU] 
4 2.209371 207.46.63.13 192.168.1.101 TCP ms-sql-s > kmscontrol [ACK] Seq=1 Ack=2 Win=5969 Len=0 
5 2.352974 192.168.1.101 207.46.63.13 TCP [TCP segment of a reassembled PDU] 
6 2.469444 207.46.63.13 192.168.1.101 TCP ms-sql-s > vaultbase [ACK] Seq=1 Ack=2 Win=8625 Len=0 

任何想法可能發生了什麼?

回答

1

我憋足了調試DomainContext : DbContext類中,發現連接字符串總是指向到SQL Express即使都在web.config中別處指向這些條目。然後我意識到問題是在構造函數中傳遞給基類的參數。

沿線的某處我認爲這個參數是要使用的數據庫的名稱。現在我意識到它的連接字符串條目的名稱(或連接字符串本身)。

+0

這正是我造成麻煩的原因。謝謝。 – Kenci 2014-12-01 20:35:13

0

找到您的計算機的IP並在SQL Azure的防火牆中創建一條規則讓其通過。它可能被阻止。

更多細節:

  1. 轉到您的Azure的儀表板。

  2. 單擊數據庫

  3. 單擊您的存儲帳戶,然後在DB服務器

  4. 單擊防火牆規則,然後添加

  5. 只要把你的IP的IP範圍的起始 IP範圍完成

+0

我已經擁有了(幸運的是儀表板告訴你它是什麼IP),我可以通過SSMS連接嗎? – 2011-06-03 10:07:59

1

我設置我的連接離子字符串中顯式實例化我的DbContext時:

public class DB: DbContext .. 

var databaseConnectionString = 
    "Server=tcp:private.database.windows.net;Database=privateDB;[email protected];Password=private;Trusted_Connection=False;Encrypt=True" 

var db = new DB(databaseConnectionString); 
相關問題