Error 40 – Could not open a connection to SQL Server

Fix SQL Server Connection Refused (Error 40) – 3 Steps

Database Errors Beginner 👁 8 views 📅 Jun 28, 2026

Your app can't reach the SQL Server database. We'll check the basics first, then dig deeper. I've seen this hundreds of times.

Why you get 'Connection Refused'

You try to connect to SQL Server from your app or SQL Server Management Studio, and you see something like:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. Server was not found or was not accessible. (Error: 40 – Could not open a connection to SQL Server)

This usually happens right after installing SQL Server, when you change network settings, or when the SQL Server service stops for no reason. I’ve seen it happen with SQL Server 2019 and 2022 on both Windows Server and Windows 10/11.

The fix is almost always something simple. Let's start with the 30-second check.

Step 1: Check if SQL Server is actually running (30 seconds)

Open Windows Services (press Win+R, type services.msc, press Enter). Look for these services:

  • SQL Server (MSSQLSERVER) – for default instance
  • SQL Server (SQLEXPRESS) – for named instance like localhost\SQLEXPRESS

If the service is stopped, right-click it and choose Start. If it's already running, skip to Step 2.

I once spent 10 minutes on a server where the service had crashed after a Windows Update. Restarting it fixed everything.

Step 2: Enable TCP/IP and SQL Server Browser (5 minutes)

This is the real fix for most people. SQL Server often comes with TCP/IP disabled by default. If you're connecting from another machine, TCP/IP must be on.

Open SQL Server Configuration Manager (search for it in Start, or run SQLServerManager15.msc for SQL Server 2019, SQLServerManager16.msc for 2022).

  1. Click SQL Server Network Configuration in the left pane.
  2. Select Protocols for MSSQLSERVER (or your instance name).
  3. Right-click TCP/IP and choose Enable.

Now, also enable the SQL Server Browser service — this lets clients find your instance by name.

  1. In the left pane, click SQL Server Services.
  2. Right-click SQL Server Browser and choose Properties.
  3. Set Start Mode to Automatic, then click Apply.
  4. Right-click SQL Server Browser again and choose Start.

Finally, restart the SQL Server service: right-click SQL Server (MSSQLSERVER) and choose Restart.

I know this tripped me up the first time too. After you restart, try connecting again. If it still fails, move to Step 3.

Step 3: Open port 1433 in Windows Firewall (15+ minutes)

Even if TCP/IP is enabled, the firewall might block the connection. This is common on cloud VMs or corporate networks.

Open Windows Defender Firewall with Advanced Security (search for it or run wf.msc).

  1. Click Inbound Rules in the left pane.
  2. Click New Rule in the right pane.
  3. Choose Port, then click Next.
  4. Select TCP, and type 1433 in Specific local ports. Click Next.
  5. Choose Allow the connection. Click Next.
  6. Check all three profiles (Domain, Private, Public). Click Next.
  7. Give the rule a name like SQL Server 1433, then click Finish.

If you're using a named instance like PCNAME\SQLEXPRESS, you also need to open UDP port 1434 for the SQL Server Browser. Repeat the steps above, but choose UDP and port 1434.

Now test the connection again. If it works, great. If not, check if your cloud provider (AWS, Azure, etc.) has its own firewall that blocks the port.

Still stuck? Try this test

Open Command Prompt as Administrator and run:

sqlcmd -S localhost

If that works, it's a network issue. If it also says connection refused, go back to Step 2 and double-check TCP/IP is enabled and the service is restarted.

I've seen cases where the SQL Server Browser service won't start because of a corrupt registry. If that happens, run sqlcmd -S localhost,1433 (the comma and port number) to bypass the browser.

When to call a professional

If none of this works, you might have a corrupted SQL Server installation. Reinstalling SQL Server can fix it, but try running a repair first from the SQL Server setup media. Or check event logs for more clues.

Most connection refused errors are just the service or TCP/IP being off. Don't panic. Run through these three steps slowly.

Was this solution helpful?