You are on page 1of 1

Public Function IsSQLServerAvailable( _

ByVal ServerAddress _
As String) As Boolean
' Tests an SQL Server connection by
' name or IP address
Try
' Attempt to get server address
Dim objIPHost As New System.Net.IPHostEntry()
objIPHost = _
System.Net.Dns.Resolve(ServerAddress)
Dim objAddress As System.Net.IPAddress
objAddress = objIPHost.AddressList(0)
' Connect to port 1433, most common SQL Server
' port. If your target is different, change here
Dim objTCP As System.Net.Sockets.TcpClient = _
New System.Net.Sockets.TcpClient()
objTCP.Connect(objAddress, 1433)
' No problems (hurrah!)
' Close and cleanup
objTCP.Close()
objTCP = Nothing
objAddress = Nothing
objIPHost = Nothing
' Return success
Return True

Catch ex As Exception
' Server unavailable, return fail value
Return False
End Try
End Function

And here's how you might call this function:

Dim blnCanConnect As Boolean


blnCanConnect = IsSQLServerOnline("maxsql001.maximumasp.com")

You might also like