A network-related or instance-specific error occurred while establishing a connection
to SQL Server ... (provider: SQL Network Interfaces, error: 26 - Error Locating
Server/Instance Specified) means the client asked for a named instance that does not
exist on that machine, or could not be found. Put the real instance name in
Server= and the same code connects.
On a developer PC with only LocalDB, that name is (localdb)\MSSQLLocalDB, not
.\SQLEXPRESS. Run sqllocaldb info to list LocalDB instances, and
check SQL Server Configuration Manager or the Windows services list for full SQL Server
instances: a service called SQL Server (SQLEXPRESS) is the instance
.\SQLEXPRESS.
The error
PS> .\FixLab.exe 'Server=.\SQLEXPRESS01;Database=master;Integrated Security=true;TrustServerCertificate=true' 'SELECT DB_NAME()'
ex.Number = -1
Microsoft.Data.SqlClient.SqlException: 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Why it happens
A server name such as .\SQLEXPRESS01 has two parts: the machine
(. is this one) and an instance name. A named instance listens on a port the
client does not know, so the client first asks the SQL Server Browser service on UDP 1434
which port that name uses. When no instance by that name answers, the client gives up and
reports error 26. It took 15 seconds here, because the lookup waits for a reply that never
comes.
The usual causes are a copied connection string that names an instance this machine does not
have (a tutorial's SQLEXPRESS on a PC with only LocalDB), a typo in the
instance name, a stopped SQL Server Browser service, or a firewall blocking UDP 1434 on a
remote server. Note that ex.Number is -1: this failure happens before any
server is reached, so there is no SQL Server error number, only the provider's error 26.
The fix
PS> sqllocaldb info
MSSQLLocalDB
PS> .\FixLab.exe 'Server=(localdb)\MSSQLLocalDB;Database=master;Integrated Security=true;TrustServerCertificate=true' 'SELECT DB_NAME()'
master
OK (-1 row(s) affected)
sqllocaldb info printed the one LocalDB instance on the machine, and the same
program connected once Server= named it. For a full SQL Server or Express
install, open SQL Server Configuration Manager, select SQL Server Services, and read the
instance name in brackets; MSSQLSERVER is the default instance, which you
address as . or localhost without a backslash. On a remote server
where the name is right, start the SQL Server Browser service or put the port in the
connection string (Server=host,1433), which skips the lookup entirely.
How it was reproduced
A console app from dotnet new console -n FixLab -f net10.0 (.NET SDK 10.0.401)
with Microsoft.Data.SqlClient 7.1.1, which opens a connection from its first argument and
prints ex.Number and the message on failure. The first attempt used
.\SQLEXPRESS and connected, because this PC happens to have an instance by that
name, so the error was reproduced with .\SQLEXPRESS01, a name that does not
exist. The fix ran against SQL Server 2022 LocalDB 16.0.1200.5 on Windows 11.
Frequently asked
- How do I fix SQL Server error 26 Error Locating Server/Instance Specified?
- Use the instance name that really exists on the target machine. On a PC with only LocalDB that is (localdb)\MSSQLLocalDB; for a full install, read the name in brackets in SQL Server Configuration Manager or the services list.
- How do I find my SQL Server instance name?
- Run sqllocaldb info for LocalDB instances. For installed SQL Server editions, open SQL Server Configuration Manager or services.msc and look for SQL Server (NAME); MSSQLSERVER is the default instance and is addressed without a name.
- Why does error 26 happen on a remote server when the instance name is right?
- The client finds a named instance's port through the SQL Server Browser service on UDP 1434. If that service is stopped or the port is blocked, start the service or put the port in the connection string, for example Server=host,1433.
More decoded errors in the Fixes category.