简体   繁体   中英

error: 40 - Could not open a connection to SQL Server

I am getting following error when trying to login on asp.net application.

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)

In my web.config file, the connection string is like

<add name="DefaultConnection" 
     connectionString="Server=1.1.1.1; Database=XYZ; Uid=user; Pwd=pswd;" 
     providerName="System.Data.SqlClient" />

Web and database server are separated. There are other websites on same web server pointing to different database on same db server, work fine.

When I double click on user name in SQL Server 2008 R2 (Security -> Users -> user) I get the following error:

在此处输入图片说明

I have checked SQL Server service and they are running fine. Just wondering if there is anything to do with this user in SQL Server.

This error is not user related. Check to ensure remote connections are enabled on SQL Server via the SQL Server Configuration Manager utility and port 1433 is open on the firewall. You can test port connectivity using TELNET or with the Powershell command below.

1433 | % { echo ((new-object Net.Sockets.TcpClient).Connect("YourServerName",$_)) "server listening on TCP port $_" }

Named Pipes protocol (see error) is only for connection on same machine and this means (probably) that on SQL Server is not enabled tcp/ip protocol for external communication.

Another possible reason for the error is the named instance of sql server; on your connection string you put the ip address of the server without instance name (better to use the machine name and put the ip on DNS or in the hosts file), this means that your sql server don't have a named instance is correct ? For check the named instance see on services.

Other possible problems maybe:

  • The firewall (on client and on server),
  • The listening port of sql server.
  • The user that attempt to connect to sql server (wrong password, disabled, no permissions on db and/or on sql instance)

I had the same problem and for some reason I had to add the standard SQL port (1433) and then everything worked. In your case that would be:

<add name="DefaultConnection" 
 connectionString="Server=1.1.1.1,1433; Database=XYZ; Uid=user; Pwd=pswd;" 
 providerName="System.Data.SqlClient" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM