2012-11-27 62 views
0

即時使用.net webservice與本地數據庫(mdf)。 這個所有的網站都運行良好,直到我將所有文件移動到另一臺計算機上並創建了一個新的iis應用程序。與SQL Server連接到數據庫mdf

System.Data.SqlClient.SqlException: 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) 
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 
    at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) 
    at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) 
    at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) 
    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) 
    at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) 
    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) 
    at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) 
    at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) 
    at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) 
    at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) 
    at System.Data.SqlClient.SqlConnection.Open() 
    at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user) 
    at System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe() 
    at System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode() 
    at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) 
    at System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression) 
    at System.Linq.Queryable.ElementAt[TSource](IQueryable`1 source, Int32 index) 

香港專業教育學院改變web配置連接字符串幾次,沒有防火牆:在這個web服務工作得很好,只是方法需要連接和查詢數據庫,會出現以下錯誤 所有方法保護。

感謝...

+0

A'。mdf'文件是一個SQL Server數據庫 - 你不能移動文件 - 你仍然需要在目標機器上安裝SQL Server(Express或其他版本)... –

回答

1

我建議幾個基礎設施的檢查,有時不僅僅是着眼於更好代碼:

  1. 確保您的SQL服務器有適當的局域網連接
  2. 做一個ping從託管您的Web服務的服務器到託管您的SQL Server的服務器
  3. 使用MAnagement Studio從不同的服務器查詢數據庫對象(網絡管理員可能會阻止重要的端口無意中)

如果可能檢查兩次你的連接字符串,有時分號可能會毀了整個事情。

編輯

連接字符串

<add name="gatewayConnectionString" 
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\Projects\p\file.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" 
providerName="System.Data.SqlClient"/> 

希望它能幫助,

+0

嗨豪爾赫,謝謝你的答案。即時通訊不使用SQL Server。它只是一個用linq查詢本地數據庫的asp應用程序。 –

+0

我明白了,遺憾的是,更新後的更好的可視性 –

+0

我的連接字符串:\t 節點 \t \t <添加名稱=「gatewayConnectionString」的connectionString =「數據源= \ SQLEXPRESS;初始目錄=網關;集成安全性= True「providerName =」System.Data.SqlClient「/> \t在其他計算機中它工作得很好...... maby它是一個iis問題? –

1

如果您使用的是MDF文件,必須有一個SQL Server或SQL Server Express上安裝的原始機器。

正如你所說你已將文件移動到另一臺計算機,你的配置顯然不再工作。您將不得不調整所有路徑信息以適應新的安裝文件夾。

此外,您將必須確保在新計算機上存在正在運行的SQL Server或SQL Server Express實例,並使其指向MDF數據庫文件。

+0

我剛剛在Visual Studio上創建了網站項目並將其附加到數據庫。除了連接字符串,我需要更改哪些路徑? –

+0

在我的其他電腦上,我只是從iis運行和配置。沒有與SQL服務器定義。 –

+0

@DanielGabay:您**必須在該機器上安裝** SQL Server Express。 –

1

MDF是標準的SQL Server的擴展,並且您正在使用的SqlClient連接,所以你肯定是使用SQL Server,但你連接到本地數據庫(MDF)的文件,而不是實際的SQL Server服務。

  • 確保您的Web應用程序正在運行的帳戶可以訪問MDF文件。

  • 確保連接字符串已連接。至少在這裏發佈,這樣我們可以提供幫助。

  • 要開始排除故障,暫時給每個人組訪問該MDF文件,看看是否有幫助。

+0

我的連接字符串:在其他電腦它工作得很好......馬比其iis問題? –

+0

此連接字符串連接到SQL Server,而不是MDF文件。它嘗試連接到與Web服務器安裝在同一服務器上的SQL Server,實例名稱爲SQLExpress。我猜測事實並非如此。您需要確定是要連接到SQL Server還是僅連接MDF文件。以下是連接到MDF文件的連接字符串示例:http://stackoverflow.com/questions/928813/connection-string-to-connect-to-mdf –