How to Fix the Error ‘A Network-related or Instance-specific Error occurred while Establishing a Connection to SQL Server’ ?
When you encounter the “Network-related or instance-specific error” in SQL Server, it usually happens when trying to connect to a database, and the connection fails unexpectedly. This error generally means that the SQL Server instance you’re trying to reach is either not running, inaccessible, or misconfigured.
The most common cause of this issue is that the SQL Server service is not running, or there are network-related problems such as firewall settings blocking the connection or incorrect SQL Server configuration. Additionally, it might be due to incorrect connection strings or instance names in your application setup.
1. Gather Information About the Instance of the SQL Server.
In this section, we will discuss ways to check whether the instance of the SQL Server is working or not, along with methods to fix it, if it is not working.
Step 1. Check if an Instance of SQL Server is Installed and is Working or Not
First of all, log in to the computer hosting the SQL server instance. Now, follow these steps to open Services in Windows.
- Click on the “Start menu” and then point to “All programs”.
- Now point to the SQL Server, and then point to “Configuration tools”.
- Click “SQL Server Configuration Manager”.
- Now select “SQL Server services” and check in the right pane whether the instance of a database engine is running or not.
- Moreover, this can be opened directly by typing “services.msc” in the RUN and clicking OK. The following screen appears.
Now, check if the database engine has been configured to accept remote connections. To check this, follow these steps:
- After services have been opened, you can see the database engine in the right pane. The “MSSQLSERVER” is a default unnamed instance. A default instance can only be one.
- In the case of “SQL Express”, the default instance will be “SQLEXPRESS” unless it is renamed by someone during installation.
- Check if the instance you are trying to connect to has the same name as given in the services.
- Also, confirm if the status of the instance is “RUNNING”.
- Moreover, if you are trying to connect to a named instance, then double-check if the “SQL Server Browser service” is already running. Thus you need to check if the “SQL Server Browser service” is started on the server on which SQL Server is installed.
- In case the database engine is not running, then you need to restart it. To start the “Database Engine”, in the right pane, right-click on the “Database Engine” (“MSSQLSERVER” default one), and then click “Start”.
Step 2. Obtain the IP address of the computer
- First of all, from the start menu, click “RUN” and type “cmd” and press OK.
- In the command prompt window, type “ipconfig” and note down the IPV4 and IPV6 addresses. People mostly use the IPV4 address.
Step 3. Get the TCP port number used by the SQL server
- Using “SQL Server Management Studio (SSMS)” connect to the instance of SQL server.
- From the “Object Explorer”, expand “Management”, expand “SQL Server Logs”, and click on the current log on which you have to apply a filter.
- To apply a filter, click apply filter and type “server is listening on” in the Message contains text box. Click apply filter and press OK.
- A message like “server is listening on [‘any’ <ipv4> 1433]” should be shown. The message shows that the SQL Server instance is listening on all computers with the IP address IPv4and the TCP port is 1433 (default).
- For more than one instance, the TCP port will be different for each instance.
- If it is not the case, then click “All programs”, point to MS SQL server configuration tools, “SQL server configuration management”, and right-click “TCP\IP” and click enable. Restart SQL server to let the changes take effect.
Method 2: Enabling protocols for port 1433
Connecting to the “Database Engine” from another computer is not allowed in many “SQL Server” implementations unless an administrator utilizes “Configuration Manager” to allow it.
- Click on the “Start menu” and then point to “All programs”.
- Point towards the “SQL Server 2008 R2”.
- Point towards “Configuration tools”, and then click “SQL Server Configuration Manager”.
- Expand “SQL Server Network Configuration”.
- Select “Protocols for MSSQL server”. Click on “TCP\IP” in the right panel.
- In the tab “Protocol” set enabled to “Yes”.
- Choose the “IP Address” tab from the window and set “TCP Port” equal to “1433” in the “IP All” entry.
- Now restart the database engine to make changes effective. To do this, from the left pane, select SQL Server services and then from the right pane, right-click the database engine instance and select “Restart”.
Method 3: Create a Firewall exception
Sometimes the Windows firewall turns on and blocks links from another computer.
- Click “Start” and start typing “Firewall.cpl” in the run box.
- You get the “Configuration Frame” for Windows Firewall by running the “firewall.cpl” command. You may turn the firewall “on/off” with exceptions and other settings applied here. Check the firewall status and turn it on to activate it if the firewall is off. If you’ve just turned this on, your firewall will block any “SQL Server” connection request to your computer at this point. By making certain exceptions, you’d need to configure the firewall to allow access to a SQL Server database engine.
- Click on “Advanced Settings”.
- We need to learn about the ports used for the “SQL Server” and the “SQL Server Browser” feature when dealing with “SQL Server” firewall configurations. Both are involved in setting up a “firewall” for the “SQL Server” . It would, therefore, be necessary to go separately through both concepts.
- You may permit or block traffic attempts that meet the requirements in the rule to access the computer. By default “inbound traffic” is blocked, you need to establish an inbound rule to allow traffic to reach the computer. Tap the Inbound Rules from the left pane of the “Windows Firewall with Advanced Security” and click the New Rule from the “Actions” window.
- Select “Port” under “Rule Type” and press the “Next” button.
- Now select “Specific local ports” and set it to 1433.
- Now select “Allow the connection” in the “Action” dialog and press the “Next” button.
- Give the rule a “Title” at this stage and press the “Finish” button.
- Select “Custom rule” from the “New rule” tab.
- Click “Customize“.
- Select “Database Engine Instance Service” from the “Customize Service Settings” under “Apply to this service” and click the “OK” button.
- Give the rule a name and click finish.
- Also add “sqlservr.exe” (typically located in “C:\Program Files (x86)\Microsoft SQL Server\MSSQL.x\MSSQL\Bin” or check your actual folder path) and port whose default value is “1433”. Also, check your connection string.
Method 4: Check Local Connection
One of the reasons for this error is providing the wrong server name, which will result in an error. As seen in the figure below, the provided server name is “DESKTOP-UD88TLT1” whereas the accurate server name is “DESKTOP-UD88TLT”. So it will be unable to connect to the server, which will result in an error “cannot connect to server”. This is the most basic reason for error, so we should check it first if working locally.
The error arises while locally connecting to SQL server with the wrong server name. In case you are using the express edition, following your server name, add “\SQLEXPRESS” as seen in the figure below.