首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 更多程式設計知識

ASP.NET 2.0 連接 SQL Server 2000 失敗:Named Pipes 錯誤 40 解決方案

     編輯:更多程式設計知識

問題情境

在 Windows XP + .NET Framework 2.0 + SQL Server 2000 的環境下,ASP.NET 應用程式嘗試連接本地資料庫時,拋出以下錯誤:

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 2005,但實際上是連接協定配置問題。預設連接字串使用 server=local 或 server=(local),會導致 ADO.NET 嘗試使用 Named Pipes 協定連接,但在某些環境下(特別是 SQL Server 2000)Named Pipes 未啟用或權限不足,導致連線失敗。

解決方法

修改 web.config 中的連接字串,將 server=local 改為 本機電腦名稱 或 127.0.0.1,強制使用 TCP/IP 協定:

<connectionStrings>
  <add name="MyConn" 
       connectionString="server=YOUR_MACHINE_NAME;database=YourDB;integrated security=sspi;" 
       providerName="System.Data.SqlClient" />
</connectionStrings>

或明確指定 TCP/IP:

server=tcp:127.0.0.1,1433;database=YourDB;integrated security=sspi;

補充建議

  • 在 SQL Server 組態管理員中,確認 Named Pipes 與 TCP/IP 協定均已啟用。
  • 若使用 SQL Server 2000,請確認服務帳戶具有網路存取權限。
  • 連接字串建議使用 Data Source 而非 server 關鍵字,語意更清晰。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved