萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

ASP.NET 2.0 連接 SQL Server 2000 常見錯誤與解決方案

在 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,但實際環境是 SQL Server 2000。核心原因在於 web.config 中的連接字符串配置不當:使用 server=local 或 server=(local) 時,Named Pipes 協議在某些網絡配置下無法正確解析本地實例名。

解決方案

將連接字符串中的 server=local 改為本地計算機名稱(或 IP 地址 127.0.0.1)。例如:

<connectionStrings>
  <add name="MyConn" 
       connectionString="Data Source=MYCOMPUTERNAME;Initial Catalog=MyDB;Integrated Security=SSPI;" 
       providerName="System.Data.SqlClient" />
</connectionStrings>

也可顯式指定 TCP/IP 協議:

Data Source=tcp:MYCOMPUTERNAME,1433;Initial Catalog=MyDB;Integrated Security=SSPI;

補充建議

  • 確保 SQL Server 2000 已啟用 TCP/IP 協議(服務器網絡實用程序中設置)。
  • 檢查防火牆是否阻擋 1433 端口。
  • 若使用 SQL Server 認證,連接字符串需包含 User ID 與 Password。
  • 生產環境建議將連接字符串加密存儲(aspnet_regiis -pef)。

通過修正連接字符串中的服務器標識,即可快速解決此類連接失敗問題。

copyright © 萬盛學電腦網 all rights reserved