473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SQL Server Existence Checking with Sockets

I wanted my programs to check to see if SQL server was available...the
following is what I cam up with:

Public Function SetServerInfo(ByVal serverName As String, ByVal dbName
As String, Optional ByVal instanceName As String = "", Optional ByVal
portNumber As Int32 = 1433) As Boolean

Dim tcpSocket As Socket
Dim resolvedServer As IPHostEntry
Dim serverEndPoint As IPEndPoint
Dim addr As IPAddress

Try
resolvedServer = Dns.Resolve(serverName)
addr = resolvedServer.AddressList(0) 'May not work on a dual
home box but I don't care
serverEndPoint = New IPEndPoint(addr, portNumber)
tcpSocket = New Socket(addr.AddressFamily, SocketType.Stream,
ProtocolType.Tcp)
tcpSocket.Connect(serverEndPoint)
If instanceName <> "" Then
_sqlServer = serverName & "\" & instanceName
Else
_sqlServer = serverName
End If
_dbName = dbName
Return True
Catch ex As Exception
_ErrorMessage = "Invalid server name, instance name or port.
Server could be offline. Contact a member of the IT support staff. "
Return False
Finally
If Not IsNothing(tcpSocket) Then
tcpSocket.Close()
End If
End Try

End Function

I would like some suggestions on improvment. More specific, I don't want to
pass the port number into this function and I want to be sure that if a
connection works that it's a SQL server that's answering. The port should be
able to be determined by some sort of port scan or something, and I would
imagine to accomplish the second thing I would need to send some sort of
packet in a way that would tell SQL to send me back a packet (I have no idea,
just random guesses).

Thanks
Nov 21 '05 #1
2 1777
Hi,

You can use the sqldmo object library to list available servers. Add
a reference to microsoft sqldmo object library in the com tab. Maybe this
will help.

Dim sqlServers As SQLDMO.NameList

Dim sqlServer As String

' Get a list of servers

sqlServers = New SQLDMO.Application().ListAvailableSQLServers

For Each sqlServer In sqlServers

Debug.WriteLine(sqlServer)

Next

Ken

-----------------
"GreyAlien007" <Gr**********@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
I wanted my programs to check to see if SQL server was available...the
following is what I cam up with:

Public Function SetServerInfo(ByVal serverName As String, ByVal dbName
As String, Optional ByVal instanceName As String = "", Optional ByVal
portNumber As Int32 = 1433) As Boolean

Dim tcpSocket As Socket
Dim resolvedServer As IPHostEntry
Dim serverEndPoint As IPEndPoint
Dim addr As IPAddress

Try
resolvedServer = Dns.Resolve(serverName)
addr = resolvedServer.AddressList(0) 'May not work on a dual
home box but I don't care
serverEndPoint = New IPEndPoint(addr, portNumber)
tcpSocket = New Socket(addr.AddressFamily, SocketType.Stream,
ProtocolType.Tcp)
tcpSocket.Connect(serverEndPoint)
If instanceName <> "" Then
_sqlServer = serverName & "\" & instanceName
Else
_sqlServer = serverName
End If
_dbName = dbName
Return True
Catch ex As Exception
_ErrorMessage = "Invalid server name, instance name or port.
Server could be offline. Contact a member of the IT support staff. "
Return False
Finally
If Not IsNothing(tcpSocket) Then
tcpSocket.Close()
End If
End Try

End Function

I would like some suggestions on improvment. More specific, I don't want to
pass the port number into this function and I want to be sure that if a
connection works that it's a SQL server that's answering. The port should
be
able to be determined by some sort of port scan or something, and I would
imagine to accomplish the second thing I would need to send some sort of
packet in a way that would tell SQL to send me back a packet (I have no
idea,
just random guesses).

Thanks
Nov 21 '05 #2
I think that you can use either SQLBrowseConnect ODBC API function or
SQL-DMO. See
http://groups.google.es/groups?hl=es...3DN%26tab%3Dwg

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
"GreyAlien007" <Gr**********@discussions.microsoft.com> escribió en el
mensaje news:AF**********************************@microsof t.com...
I wanted my programs to check to see if SQL server was available...the
following is what I cam up with:

Public Function SetServerInfo(ByVal serverName As String, ByVal dbName
As String, Optional ByVal instanceName As String = "", Optional ByVal
portNumber As Int32 = 1433) As Boolean

Dim tcpSocket As Socket
Dim resolvedServer As IPHostEntry
Dim serverEndPoint As IPEndPoint
Dim addr As IPAddress

Try
resolvedServer = Dns.Resolve(serverName)
addr = resolvedServer.AddressList(0) 'May not work on a dual
home box but I don't care
serverEndPoint = New IPEndPoint(addr, portNumber)
tcpSocket = New Socket(addr.AddressFamily, SocketType.Stream,
ProtocolType.Tcp)
tcpSocket.Connect(serverEndPoint)
If instanceName <> "" Then
_sqlServer = serverName & "\" & instanceName
Else
_sqlServer = serverName
End If
_dbName = dbName
Return True
Catch ex As Exception
_ErrorMessage = "Invalid server name, instance name or port.
Server could be offline. Contact a member of the IT support staff. "
Return False
Finally
If Not IsNothing(tcpSocket) Then
tcpSocket.Close()
End If
End Try

End Function

I would like some suggestions on improvment. More specific, I don't want
to
pass the port number into this function and I want to be sure that if a
connection works that it's a SQL server that's answering. The port should
be
able to be determined by some sort of port scan or something, and I would
imagine to accomplish the second thing I would need to send some sort of
packet in a way that would tell SQL to send me back a packet (I have no
idea,
just random guesses).

Thanks

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: siliconmike | last post by:
Hi, I'm looking for a reliable script that would connect to a host and somehow determine whether an email address is valid. since getmxrr() only gets the mx records.. Links/pointers ? Mike
5
by: daniel.shaya | last post by:
I'll try and keep this brief so in a nutshell: I have large distributed java system running on a Windows 2003 server (4cpu 8Gb memory). Periodically the following exceptions occurs in the...
5
by: Richard L Rosenheim | last post by:
What's the proper technique for checking for the existence of an attribute within a node? Lets say I did a SelectSingleNode which returned this element: <AnAddress city="San Francisco"...
4
by: Herbert VON GRÜNENWALD | last post by:
hi everyone, i've made a client/server class in C++ and some other to do the same thing in c#. clients and server works fine, in the two langages, but there is one problem when a c++ client...
0
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
25
by: _DD | last post by:
I'd like to include a 'Test Connection' button in an app, for testing validity of a SQL connection string. I'd prefer to keep the timeout low. What is the conventional way of doing this?
0
by: alikim | last post by:
Hi, I'm trying to create a network game where the server part is a cgi script and the client is a flash embedded into an html page. Data exchange is performed via sockets. Now I'm using the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.