When using SQL Server, many beginners will encounter the problem that SQL Server Management Studio cannot connect to a remote database instance. The error is described as follows:
An error has occurred while establishing a connection to the server.
(Provider: Named Pipes Provider, error: 40-cocould not open a connection to SQL Server) (Microsoft SQL Server, Error: 5)
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-cocould not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)
This means that a connection cannot be established between databases because the named pipeline provider has an error. In fact, this is a typical Database Server SETUP problem, which may be encountered in the LAN or WAN. We only need to configure the database server to solve this problem, let's take a look at the specific steps.
Make sure that the database service on the server has been started
Start> All Programs> Microsoft SQL Server 2008> Configutation Tools. Open SQL Server Configuration Manager and click SQL Server Services to check whether the database service has been started. if the service is not enabled, start it manually. Of course, you can also choose Start> Control Panel> Administrative Tools> services in Windows to check whether the corresponding database service is enabled. Or if the Server and your machine are in the same network, you can also use the command "sqlcmd-L" to note that L should be capitalized) to view all the available SQL Server servers in the network.
Enable TCP/IP in SQL Server Configuration
TCP/IP support is required for communication between multiple SQL Server servers over the network. To enable the SQL Server to be remotely connected, the TCP/IP must be enabled. Follow the steps described above to open SQL Server Configuration Manager, enable SQL Server Network Configuration, select the database you want to set, click TCP/IP, and right-click Enable. If necessary, you can also enable Named Pipes. Remember, all modifications take effect only after the SQL Server service is restarted!
Port Number for enabling SQL Server in Windows Firewall
In many cases, we still cannot establish a remote connection after many settings on the database server itself. In this case, we need to consider whether the firewall is at fault. By default, many port numbers and services are disabled by the firewall and cannot be accessed or executed remotely. The default port number of SQL Server is no exception. We should reset Windows Firewall to add exceptions to SQL Server. Unless manually modified, the default port number of SQL Server is 1433. Add the port number to Windows Firewall. If SQL Server has a named instance, you should also add SQL Server browser to Windows Firewall. For SQL Server naming instances, we will introduce them later)
Open the Windows control panel and choose Windows Firewall> Change Settings> Exceptions> Add Port.
Click Add port... in the pop-up dialog box, fill in:
Name: SQL
Port Number: 1433
Protocol: Select TCP
Enable remote connection in SQL Server Manager
This step is usually ignored by the database administrator. If remote connection is not enabled, the database instance can only be connected locally but not remotely. enabling remote connection is equally important. By default, remote connection is disabled. For example, open SQL Server Management Studio, right-click the database instance, and select the properties menu.
In the displayed window, select Connections on the left and select "Allow remote connections to this server ".
Enable the SQL Server Browser Service
If SQL Server is not installed with the default Instance name but a custom Instance name, and the specified TCP/IP Port Number is not configured, as described above, SQL Server still does not support remote connection. However, If you enable the SQL Server Browser service, you can use the dynamic TCP/IP Port Number to remotely connect to the SQL Server. Enabling SQL Server Browser is very simple. Similar to enabling SQL Server, right-click SQL Server Browser in SQL Server Configuration Manager and choose enable. Enabling this service will affect all installed SQL Server instances on the Server.
Create an exception for the sqlbrowser.exe application in the fire wall
As we have mentioned earlier, to support remote connections to custom-named SQL Server instances, you must enable the sqlbrowser service. Windows Firewall may prevent the service from being executed. Therefore, you must add exceptions to the sqlbrowser service in Windows Firewall.
First, find the path to install the sqlbrowser.exe Program on the Server, such as C: \ Program Files \ Microsoft SQL Server \ 90 \ Shared \ sqlbrowser.exe. If you are not sure where SQL Server is installed, search for the file name in Windows. As described earlier, add an SQL TCP/IP Port number to the firewall and add firewall exceptions to the sqlbrowser.exe application.
Recreate a database alias
It is common to create an SQL Server alias and use it in applications. The database alias can be used to ensure that once the database location changes, such as a new server or IP address, the database connection string in the application does not need to be modified. Otherwise, if you change the database location, you need to notify all applications using the database to modify the source code or the connection strings in the configuration file. This is probably impossible. Therefore, it is wise to use the database alias to configure the connection string. In addition, you can use the same alias to direct to different database instances. When modifying the alias parameter, you can immediately switch between databases. Creating a database alias is very simple. Select Aliases in SQL Server Configuration Manager to create a database alias.