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

Monday, September 1, 2025

How to Fix SQL Server Connection Errors (e.g., Error 18456)?

 

Connectivity issues rank high in searches, stemming from auth failures, network problems, or config errors.

Scenario: A web app can't connect to SQL Server after a server migration, showing "Login failed for user" errors.

Detailed Solution with Code Examples:

  • Step 1: Check Error Log. View via SSMS (Object Explorer > SQL Server Logs) or:
    sql
    EXEC xp_readerrorlog 0, 1, N'Login failed';
  • Step 2: Verify Authentication. For SQL auth, reset password:
    sql
    ALTER LOGIN YourLogin WITH PASSWORD = 'NewPassword';
  • Step 3: Enable Protocols. Use SQL Server Configuration Manager to enable TCP/IP, restart service.
  • Step 4: Test Connection. Use PowerShell:
    powershell
    $conn = New-Object System.Data.SqlClient.SqlConnection
    $conn.ConnectionString = "Server=YourServer;Database=YourDB;Integrated Security=True;"
    try { $conn.Open(); Write-Host "Connected!"; } catch { Write-Host $_.Exception.Message; }
  • Step 5: Fix Firewall/Port Issues. Ensure port 1433 is open; for dynamic ports, query:
    sql
    SELECT DISTINCT local_tcp_port FROM sys.dm_tcp_listener_states;

Best Practices:

  • Use Windows auth over SQL for security.
  • Implement connection pooling in apps to reduce overhead.
  • Monitor failed logins with audits: CREATE SERVER AUDIT FailedLogins TO FILE (FILEPATH = 'C:\Audits\');.
  • In Azure, check NSG rules.
  • Test connections post-updates; use aliases for flexibility.

This resolves 80% of issues quickly, ensuring reliable access.




No comments:

Post a Comment

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

Post Bottom Ad

Responsive Ads Here