在使用 ASP.NET 2.0 搭配 SQL Server 2000 開發系統時,執行專案卻出現以下錯誤訊息:
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
明明使用的是 SQL Server 2000,錯誤訊息卻提示「connecting to SQL Server 2005」,令人困惑。
安裝 Visual Studio 2005 時,會一併安裝 SQL Server 2005 的相關元件(如 SQL Server 2005 Express Edition)。這導致系統在處理資料庫連線請求時,預設改由 SQL Server 2005 的服務來處理。而 SQL Server 2005 預設設定不允許遠端連線,因此拋出錯誤 40。
sqlservr.exe 通訊。若專案確定只需使用 SQL Server 2000,也可在連線字串中明確指定 Data Source=伺服器名稱;Initial Catalog=資料庫名;Integrated Security=SSPI;,並確認 sqlncli 或 SQLOLEDB 提供者正確指向 SQL Server 2000 執行個體,避免被導向 SQL Server 2005 服務。
此問題常見於並存 SQL Server 2000 與 2005 的開發環境,正確設定網路協定與防火牆即可解決。