2016-04-27 66 views
1

使用SSMS和Visual Studio 2015年的Server ExplorerData Connections標籤下,我可以執行查詢在遠程服務器KOSH沒有問題。爲什麼在Visual Studio/IIS Express中本地運行的MVC應用程序無法做到這一點?的Visual Studio連接到數據庫,但使用內相同的連接字符串應用程序不能

使用VS2015的連接Properties,我得到它的連接字符串:

Data Source=123.456.78.9;Persist Security Info=True;User ID=Foo;Password=Bar 

使用該連接字符串,MVC應用招呼以:

"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)"

內部異常消息:

"The network path was not found"

我知道服務器/防火牆/網絡設置是正確的。這留下了應用程序。

using(var connection = Database.Connection(conInfo.ConnectionString, provider)) { 
    // rowCountSql: SELECT SUM(rows) TM_ROWCOUNT FROM sys.partitions 
    //    WHERE object_id = object_id(@tableName) and index_id IN (0,1) 
    // Any other SQL yields same error 
    var countTask = connection.QueryAsync(rowCountSql, new { tableName = Editor.TableName }); 
} 

使用異步,因爲Oracle版本的查詢可能需要幾秒鐘才能返回。 應用程序應該是正確的,因爲它從家中(也是服務器的位置)連接到同一臺服務器,並且在運行2008R2到2014的遠程數據中心中的相同數據庫中沒有問題。

Database.Connection()是:

// This is unlikely to be the problem as it is very well tested 
public static DbConnection Connection(string connectionString, string databaseProvider) { 
    DbProviderFactory databaseFactory = DbProviderFactories.GetFactory(databaseProvider); 
    DbConnection connection = databaseFactory.CreateConnection(); 
    if(connection != null) 
     connection.ConnectionString = connectionString; 
    return connection; 
} 

我敢打賭,我失去了一些東西簡單,但我會很感激的幫助是什麼,可能是。

+0

你確定你的conInfo.ConnectionString是正確的嗎? – Henrique

+0

您的Oracle DB沒有名稱嗎?使用IP地址通常是高級選項。 – silkfire

+1

@Henrique:我有很多奇妙的原因可以肯定'conInfo.ConnectionString'包含我一直在編輯的字符串,所有這些都顯然是錯誤的,因爲這是問題所在。 (臉蛋)。 謝謝。如果您發佈的是完整回覆,我會將其標記爲正確,因爲「無論如何檢查明顯的」往往是解決方案。 –

回答

0

正如我在評論說,可以將一塊代碼的:

conInfo.ConnectionString 

不包含要正確的連接字符串。

檢查出來

相關問題