Solved

How to check if local sql server exist or any other sql server exist by passing the name?

Posted on 2006-06-27
2
204 Views
Last Modified: 2010-04-07
Need VB code to check whether a sql server with certain name exist or not. Please help!
0
Comment
Question by:Skaugen
2 Comments
 
LVL 12

Expert Comment

by:jkaios
ID: 16997873
Have you tried the Microsoft SQL DMO object library.
It has dozens of exposed properties and methods including functions to
list all available SQL Servers on your network.

If you install SQL Server (just the client tools) on your machine then it will be
available in the References list.
0
 
LVL 1

Accepted Solution

by:
maonas earned 500 total points
ID: 17000566
Here is the code; where strMachineName is the name of the SQL Server that you want to check.

Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object
Dim strMachineName As String

strMachineName = "localhost"

On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strMachineName & "\root\cimv2")
If Err.Number Then
    ' Unable to connect to server
End If

Dim strQuery As String
strQuery = "SELECT * FROM Win32_Service WHERE Name = 'MSSQLServer'"
Set colItems = objWMIService.ExecQuery(strQuery, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)
If Err.Number Then
    ' Unable to get SQL Server status
Else
    If colItems.Count Then
        For Each objItem In colItems
            Debug.Print "Status: " & objItem.Status
            Debug.Print "State: " & objItem.State
        Next
    Else
       'MSSQLServer service not running on strMachineName
    End If
End If
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now