Solved

How to detect that SQL Server is installed in Local Machine

Posted on 2006-06-13
6
404 Views
Last Modified: 2011-10-03
how to detect that SQL server or MSDE  is installed on the Local Machine
I want to do that in my program  using VB.NET

thanks
0
Comment
Question by:assaadrezk
6 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 250 total points
Comment Utility
The simplest way is to attempt to open a connection.  If it works, it's there.  If not, then it's not there or you're doing something wrong.
0
 

Author Comment

by:assaadrezk
Comment Utility
there is other reasons not to connect to database , before try to connect I want to make sure that database engine is installed because I'm going to ask about the instance name
0
 
LVL 25

Expert Comment

by:SStory
Comment Utility
How about searching for the
HKLM/Software/Microsoft/Microsoft SQL Server key in the registry?

Or search program files directory for sqlservr.exe
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:assaadrezk
Comment Utility
good idea, you have the code doing that
0
 
LVL 1

Expert Comment

by:thebatdude
Comment Utility
First, check for the existence of this key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MSSQLSERVER
OR
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MSSQL$[INSTANCENAME]

Look at the "ImagePath" string entry under this key.  It will point to the SQL Server service

If this key exists, then you can then try to connect to it using:
Data Source=(local);Integrated Security=SSPI;Connect Timeout=15;
OR
Data Source=(local)\INSTANCENAME;Integrated Security=SSPI;Connect Timeout=15;

NOTE:  you may need to replace the word (local) with the actual machine name, in some rare cases, (local) does not resolve because people have changed their local DNS mapping.

If this timesout then one of the following could be wrong:
1: The MSSQLSERVER service is not running or
2: the current user is not a member of the administrator group, or any other group which has login permissions on the sql server.  NOTE: in this case, you will get an access denied.  
3:  The sql server service is running, the user does have permissions but is currently logged into a computer using cached network credentials (the network cable has been unplugged) and their access to the SQL Server is based on a network set of credentials.  SQL Server does not cache credentials unlike Windows, which will allow you to login to a machine off network (as long as you have logged int before).
4: The maximum number of connections has been exceeded.

For MSDE, you will need to search for all keys that begin with MSSQL$ under this hive:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\

Same deal applies here, you could get denied access for any of the above reasons.  The only way I know how to determine the difference between a full blown SQL Server and an MSDE instance is to call: xp_msver



0
 
LVL 25

Expert Comment

by:SStory
Comment Utility
Actually I do.  Below is a class that I wrote in VB.NET 2005 to do registry queries. It is tailored to what I was trying to accomplish, but should give you an idea.  Obviously I retain all rights to this code in terms of it still being mine, but grant readers the right to use it:

Imports Microsoft.Win32
Public Class RegSearchItem
    Private DefaultValue As String
    Public PropertyName As String
    Public RegValueName As String
    Public Value As String
    Public RegHive As RegistryHive
    Public SubKey As String 'not including hive should be like Software\Microsoft\Windows or something

    'PropertyName allows you to give it a different value to write to the XMLAttribute than what it was in the registry
    'RegValueName, must be the name of the value you are looking for in the registry
    'if you don't specify PropertyName, it is =RegValueName
    Sub New(ByVal RegHive As RegistryHive, ByVal SubKey As String, ByVal RegValueName As String, Optional ByVal PropertyName As String = "", Optional ByVal DefaultValue As String = "")
        Me.RegValueName = RegValueName
        Me.SubKey = SubKey
        Me.RegHive = RegHive
        If PropertyName = "" Then
            Me.PropertyName = RegValueName
        Else
            Me.PropertyName = PropertyName
        End If

        Me.DefaultValue = DefaultValue
    End Sub

    Sub QueryRegistry()
        Dim key As RegistryKey

        'get the sub key from the given hive
        Select Case RegHive
            Case RegistryHive.ClassesRoot
                key = My.Computer.Registry.ClassesRoot.OpenSubKey(SubKey)
            Case RegistryHive.CurrentConfig
                key = My.Computer.Registry.CurrentConfig.OpenSubKey(SubKey)
            Case RegistryHive.CurrentUser
                key = My.Computer.Registry.CurrentUser.OpenSubKey(SubKey)
            Case RegistryHive.DynData
                key = My.Computer.Registry.DynData.OpenSubKey(SubKey)
            Case RegistryHive.LocalMachine
                key = My.Computer.Registry.LocalMachine.OpenSubKey(SubKey)
            Case RegistryHive.PerformanceData
                key = My.Computer.Registry.PerformanceData.OpenSubKey(SubKey)
            Case RegistryHive.Users
                key = My.Computer.Registry.Users.OpenSubKey(SubKey)
            Case Else
                key = Nothing
        End Select

        If Not key Is Nothing Then
            'get the value of the given property, under this subkey
            Value = key.GetValue(RegValueName)
            key.Close()
        Else
            Value = DefaultValue
        End If
    End Sub
End Class

HTH,

Shane
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the most frequently asked questions on EE in the "Windows Installer" zone is how to eliminate self-triggered installation of some product.  The problem occurs when, suddenly, whenever a certain application is launched, or even when a folder i…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

11 Experts available now in Live!

Get 1:1 Help Now