Hi,

I looking for a solution for how to check if a connection to a remote
computer exist or not using vb.net.

I 'm writing a program which will request data from a remote sql server. I
need to check if the connection to the remote server exist, before open the
connection or run the the sql command.

Pls helps, urgent.

Thanks,

charlie

Dani AI

Generated

— for an online app that must not hang when the DB is unreachable, the right approach is to fail fast and verify the server quickly before doing a full open. is correct in the literal sense (if Open() succeeds you are connected) but that does not help avoid long default timeouts. Use a lightweight pre-check plus a short Connect Timeout or an async open so the UI/thread doesn’t block.

Typical, practical options:

  • TCP port probe (fast, reliable for TCP-accessible servers).
  • ICMP Ping (simple but often blocked by firewalls).
  • Attempt a SqlConnection.Open with a short Connect Timeout and immediately run a simple query (for full verification).

Example: TCP port probe (non-blocking timeout)

' Quick TCP port probe
Dim host As String = "sql.example.com"
Dim port As Integer = 1433
Using tcp As New System.Net.Sockets.TcpClient()
    Dim ar = tcp.BeginConnect(host, port, Nothing, Nothing)
    Dim ok = ar.AsyncWaitHandle.WaitOne(2000) ' 2s timeout
    If ok Then
        tcp.EndConnect(ar)
        ' likely reachable
    Else
        tcp.Close()
        ' unreachable or port closed
    End If
End Using

Example: Open with a short Connect Timeout and verify with SELECT 1

Dim csb As New System.Data.SqlClient.SqlConnectionStringBuilder()
csb.DataSource = "sql.example.com,1433"  ' specify port to avoid discovery delays
csb.InitialCatalog = "master"
csb.UserID = "readonly"
csb.Password = "pwd"
csb.ConnectTimeout = 5

Using cn As New System.Data.SqlClient.SqlConnection(csb.ConnectionString)
    Try
        cn.Open()
        Using cmd As New System.Data.SqlClient.SqlCommand("SELECT 1", cn)
            cmd.CommandTimeout = 5
            Dim ok = cmd.ExecuteScalar()
        End Using
    Catch ex As Exception
        ' handle timeout/connection failure
    End Try
End Using

Notes and cautions:

  • Named instances may use dynamic ports; specifying "server,port" avoids the extra SQL Browser lookup (UDP 1434) that can add delay.
  • Ping can be blocked; TCP probe is usually a better indicator for SQL Server.
  • Windows Authentication can introduce delays if domain controllers are unreachable — consider using a low-privilege SQL account for availability checks.
  • Run checks off the UI thread or use OpenAsync with a CancellationToken to keep the UI responsive. Use short timeouts, exponential backoff, and cache the last-known-good state to avoid repeated long waits.

Recommended Answers

All 2 Replies

If your connection is successful using the connection string then it is connected.

I am doing online application. When off line. My application will take a long time to connect to SQL Server until the timeout.

So I want that check the server is online then just connect to SQL Server.

Any solutions, please help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.