Share via

Connecting to SQLExpress Database takes long time

Lorne Johnson 0 Reputation points
2025-05-09T18:00:30.5266667+00:00

Upgraded .net 4.8 C# app to .net 8. The new version takes 30 seconds to connect to SQLExpress database on server (\ServerName\SQLExpress). Original version took less than 1 second to connect to same server. Management Studio and reporting tool also take less than 1 second to connect. Built a small app in .net 6 and 8 and both take 30 seconds to connect. Once the first connection completes all of the subsequent connections are less than a second. Looking for advice as to where I can look to figure out where the delay is cominmg from. I believe I have ruled out network traffic and the server. Seems .net 6/8 first connection is taking a long time to resolve but once it does it is fine.

Here are the commands I am using:

        CPLdbconnect = ("Data Source = Server\\sqlexpress; " +

        "Initial Catalog = CPL; " +

        "Persist Security Info = True; " +

        "User ID = userid; " +

        "Password = password; " +

        "TrustServerCertificate = True; " +

        "Connect Timeout = 60; ");

.net 6 - using System.Data.SqlClient.SqlConnection con = new(CPLdbconnect);

.net 8 - using Microsoft.Data.SqlClient.SqlConnection con = new(CPLdbconnect);

con.Open();

Developer technologies | .NET | .NET Runtime

2 answers

Sort by: Most helpful
  1. Tomaž Žveglič 0 Reputation points
    2026-03-05T14:10:32.9766667+00:00

    If your connection to SQL Server is fast locally (~0.3s) but takes ~5 seconds from another machine in the same LAN, and you're using IP + port (e.g. 192.168.1.10,1433), the issue may not be SQL Server itself.

    In my case, I was using:

    • Microsoft SQL Server 2022
    • Microsoft.Data.SqlClient
    • SQL authentication (sa)
    • Encrypt=True and TrustServerCertificate=True

    Telnet to port 1433 was instant, so network connectivity was fine. However, connection.Open() consistently took ~5 seconds.

    After running Wireshark, I noticed this pattern before the TDS pre-login:

    • TCP handshake (instant)
    • Several NBNS (NetBIOS Name Service) broadcasts
    • ICMP “Port Unreachable” responses
    • Only then TDS pre-login started

    The delay happened during those repeated NBNS attempts.

    Root Cause

    Windows was attempting NetBIOS name resolution before proceeding, even though I was connecting via IP address. Because no WINS server was present, it retried multiple times and waited for timeouts (~5 seconds total).

    Fix

    On the client machine, disable NetBIOS over TCP/IP:

    1. Open Network Connections
    2. Right-click your active adapter → Properties
    3. Select Internet Protocol Version 4 (TCP/IPv4) → Advanced
    4. Go to the WINS tab
    5. Select: Disable NetBIOS over TCP/IP
    6. Reboot (not required)

    After disabling NetBIOS, the connection time dropped from ~5 seconds to <0.5 seconds.

    Why This Happens

    If:

    • Machines are in a workgroup (no domain)
    • No WINS server exists
    • NetBIOS is enabled

    Windows may try legacy name resolution and wait for timeouts before continuing. This delay appears at the application level as a slow Open() call.


  2. Bruce (SqlWork.com) 83,741 Reputation points
    2025-05-19T21:19:37.78+00:00

    .net core uses tcp/ip to connect. this can be an issue when connecting to a named instance. its assumed a named instance is using a dynamic port, so it must connect to sql browser service to get the dynamic port. but you are probably not running the browser service.

    to speed up connection, have SQLExpress support a fixed port, and specify in the connect string:

    https://dotnet.territoriali.olinfo.it/en-us/troubleshoot/sql/database-engine/connect/static-or-dynamic-port-config

    if you need dynamic ports be sure the sqlbrowser is setup correctly:

    https://dotnet.territoriali.olinfo.it/en-us/sql/tools/configuration-manager/sql-server-browser-service?view=sql-server-ver16

    note: you old code is probably using named pipes (netbios) to connect.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.