Md Mominul Islam | Software and Data Enginnering | SQL Server, .NET, Power BI, Azure Blog

while(!(succeed=try()));

LinkedIn Portfolio Banner

Latest

Home Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Tuesday, September 2, 2025

Fix SQL Server Error 40, 26, 10054: Resolve Network-Related or Instance-Specific Errors

 

Introduction

SQL Server Error 40 ("Could not open a connection to SQL Server"), Error 26 ("Error locating server/instance specified"), and Error 10054 ("An existing connection was forcibly closed by the remote host") indicate network-related or instance-specific issues when connecting to SQL Server. These errors typically stem from network configuration, SQL Server instance issues, or firewall settings. This guide provides code-oriented solutions to diagnose and resolve these errors systematically.


1. Verify SQL Server Service Status

Ensure the SQL Server instance and SQL Server Browser service are running.

Code-Oriented Solution

Check service status using PowerShell:

# Check SQL Server service status
Get-Service -Name "MSSQLSERVER" | Select-Object Name, Status

# Check SQL Server Browser service
Get-Service -Name "SQLBrowser" | Select-Object Name, Status

Action:

  • If stopped, start the services:

# Start SQL Server service
Start-Service -Name "MSSQLSERVER"

# Start SQL Server Browser
Start-Service -Name "SQLBrowser"
  • For named instances, ensure the SQL Server Browser service is running to resolve instance names to ports.

Why? Errors 40 and 26 occur if the SQL Server instance or Browser service is not running, preventing connections.


2. Verify Network Connectivity

Check if the SQL Server host is reachable and the port is open.

Code-Oriented Solution

Test network connectivity using PowerShell:

# Ping the server
Test-Connection -ComputerName "sqlserverhostname" -Count 2

# Test TCP port (default: 1433)
Test-NetConnection -ComputerName "sqlserverhostname" -Port 1433

Action:

  • If the ping fails, verify the server’s IP address or DNS resolution:

# Check DNS resolution
Resolve-DnsName -Name "sqlserverhostname"
  • If the port is not open, check firewall settings (see Step 4).

  • For named instances, find the dynamic port:

-- Check current port
SELECT DISTINCT local_tcp_port
FROM sys.dm_exec_connections
WHERE local_tcp_port IS NOT NULL;

Why? Error 10054 (connection forcibly closed) or Error 26 (instance not found) can occur due to network issues or blocked ports.


3. Check SQL Server Instance Configuration

Ensure the instance is configured correctly and listening on the expected port.

Code-Oriented Solution

Verify TCP/IP is enabled and check the port in SQL Server Configuration Manager:

-- Check if TCP/IP is enabled (run in SQLCMD or SSMS if accessible)
SELECT 
    SERVERPROPERTY('InstanceName') AS InstanceName,
    SERVERPROPERTY('IsClustered') AS IsClustered
FROM sys.dm_server_services;

Action:

  • Open SQL Server Configuration Manager:

    1. Navigate to SQL Server Network Configuration > Protocols for <instance_name>.

    2. Ensure TCP/IP is enabled.

    3. Under TCP/IP Properties > IP Addresses, verify the port (e.g., 1433) or check for dynamic ports.

  • For named instances (e.g., ServerName\InstanceName), ensure SQL Server Browser is running.

  • Set a static port if needed:

-- Example: Set static port in SQL Server Configuration Manager
-- Set TCP Port to 1433 and clear TCP Dynamic Ports

Why? Error 26 often occurs when the instance is not found due to disabled protocols or incorrect port settings.


4. Check Firewall Settings

Ensure SQL Server ports (default: 1433 for TCP, 1434 for UDP) are open.

Code-Oriented Solution

Add firewall rules using PowerShell:

# Allow TCP port 1433 for SQL Server
New-NetFirewallRule -DisplayName "Allow SQL Server TCP 1433" -Direction Inbound -Protocol TCP -LocalPort 1433 -Action Allow

# Allow UDP port 1434 for SQL Server Browser
New-NetFirewallRule -DisplayName "Allow SQL Browser UDP 1434" -Direction Inbound -Protocol UDP -LocalPort 1434 -Action Allow

# Allow sqlservr.exe
New-NetFirewallRule -DisplayName "Allow SQL Server Executable" -Direction Inbound -Program "C:\Program Files\Microsoft SQL Server\MSSQLXX.MSSQLSERVER\MSSQL\Binn\sqlservr.exe" -Action Allow

Action:

  • Verify firewall rules on both server and client machines.

  • Check network firewalls or security appliances for additional restrictions.

  • For dynamic ports, ensure the SQL Server Browser service is allowed through UDP 1434.

Why? Blocked ports cause Error 40 (cannot connect) or Error 10054 (connection closed).


5. Validate Connection String

Ensure the connection string is correct and specifies the correct instance and database.

Code-Oriented Solution

Test connection strings in different formats:

// Default instance with Windows Authentication
string connString = "Server=sqlserverhostname;Database=YourDatabaseName;Integrated Security=True;";

// Named instance with SQL Authentication
string connString = "Server=sqlserverhostname\\InstanceName;Database=YourDatabaseName;User Id=YourLoginName;Password=YourPassword;";

// Specific port
string connString = "Server=sqlserverhostname,1433;Database=YourDatabaseName;Integrated Security=True;";

Test the connection in C#:

using System.Data.SqlClient;

string connString = "Server=sqlserverhostname;Database=YourDatabaseName;Integrated Security=True;";
try {
    using (SqlConnection conn = new SqlConnection(connString)) {
        conn.Open();
        Console.WriteLine("Connection successful!");
    }
} catch (SqlException ex) {
    Console.WriteLine($"Error {ex.Number}: {ex.Message}");
}

Action:

  • If using a hostname, try the IP address to rule out DNS issues.

  • Specify the port explicitly for static port instances.

  • Increase the connection timeout if needed:

string connString = "Server=sqlserverhostname;Database=YourDatabaseName;Integrated Security=True;Connection Timeout=30;";

Why? Incorrect connection strings cause Error 26 (instance not found) or Error 40 (cannot connect).


6. Check SQL Server Authentication and Permissions

Ensure the login is valid and has access to the server and database.

Code-Oriented Solution

Verify the login exists and has permissions:

-- Check server logins
SELECT name, type_desc, default_database_name
FROM sys.server_principals
WHERE name = 'YourLoginName';

-- Check database permissions
USE YourDatabaseName;
GO
SELECT 
    dp.name AS DatabaseUser,
    sp.name AS ServerLogin,
    permission_name
FROM sys.database_permissions p
JOIN sys.database_principals dp ON p.grantee_principal_id = dp.principal_id
LEFT JOIN sys.server_principals sp ON dp.sid = sp.sid
WHERE dp.name = 'YourUserName';

Action:

  • Create or map the login if missing:

-- Create SQL Server login
CREATE LOGIN YourLoginName WITH PASSWORD = 'YourSecurePassword';

-- Map to database user
USE YourDatabaseName;
GO
CREATE USER YourUserName FOR LOGIN YourLoginName;
GRANT CONNECT TO YourUserName;
  • For Windows Authentication, ensure the account is mapped:

CREATE USER [DOMAIN\YourUserName] FOR LOGIN [DOMAIN\YourUserName];
GRANT CONNECT TO [DOMAIN\YourUserName];

Why? Invalid or unauthorized logins can cause connection failures, contributing to Error 40.


7. Test with SQLCMD or SSMS

Test connectivity using SQL Server Management Studio (SSMS) or sqlcmd.

Code-Oriented Solution

Test with sqlcmd:

# Test default instance
sqlcmd -S sqlserverhostname -E -Q "SELECT @@VERSION"

# Test named instance
sqlcmd -S sqlserverhostname\InstanceName -U YourLoginName -P YourPassword -Q "SELECT @@VERSION"

In SSMS, connect using the server name or IP and specify the instance.

Action:

  • If the connection fails, note the exact error for further diagnosis.

  • Try connecting to the master database to rule out database-specific issues:

sqlcmd -S sqlserverhostname -E -d master

Why? Testing isolates whether the issue is server-wide, instance-specific, or network-related.


8. Analyze SQL Server Logs

Check logs for detailed information on the connection failure.

Code-Oriented Solution

Query the error log:

-- Read error log for connection issues
EXEC xp_readerrorlog 0, 1, N'Login failed', NULL, NULL, NULL, N'asc';
EXEC xp_readerrorlog 0, 1, N'10054', NULL, NULL, NULL, N'asc';

Action:

  • Review logs at: C:\Program Files\Microsoft SQL Server\MSSQLXX.MSSQLSERVER\MSSQL\Log\ERRORLOG.

  • Look for details about failed logins, network issues, or instance errors.

Why? Logs provide specific insights into the cause of Errors 40, 26, or 10054.


9. Advanced Troubleshooting

Address less common causes, such as network resets or instance configuration issues.

Code-Oriented Solution

Check for network resets (Error 10054) using PowerShell:

# Monitor network connectivity
Test-NetConnection -ComputerName "sqlserverhostname" -Port 1433 -InformationLevel Detailed

Verify SQL Server instance configuration:

-- Check server configuration
SELECT 
    SERVERPROPERTY('ServerName') AS ServerName,
    SERVERPROPERTY('InstanceName') AS InstanceName,
    SERVERPROPERTY('IsClustered') AS IsClustered
FROM sys.dm_server_services;

Action:

  • For Error 10054, check for network interruptions (e.g., VPN drops, firewall rules, or load balancers).

  • Ensure SQL Server is not running in a clustered environment with misconfigured failover settings.

  • For named instances, specify the port explicitly in the connection string if SQL Server Browser is disabled:

string connString = "Server=sqlserverhostname,1433;Database=YourDatabaseName;Integrated Security=True;";

Why? Network resets or misconfigured instances can cause intermittent connection failures.


Conclusion

SQL Server Errors 40, 26, and 10054 (network-related or instance-specific) can be resolved by verifying service status, network connectivity, instance configuration, firewall settings, and authentication. Use the provided T-SQL, PowerShell, and C# examples to diagnose and fix these issues systematically. 

No comments:

Post a Comment

Thanks for your valuable comment...........
Md. Mominul Islam

Post Bottom Ad

Responsive Ads Here