Results 1 to 3 of 3

Thread: Search for DataSourceName

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    2

    Question

    Hi:

    I would like to check for a DSN in the registry. I've searched through the relevant articles and posts here but I still don't get how to do it.

    I've tried using RegQueryValue which seems to be close to what I want. I snagged the following to try to retrieve the name of the database. What I'd really like to do is find out if the datasource is there.

    Dim hKey As Long ' receives a handle to the newly created or opened registry key
    Dim subkey As String ' name of the subkey to open
    Dim stringbuffer As String ' receives data read from the registry
    Dim datatype As Long ' receives data type of read value
    Dim slength As Long ' receives length of returned data
    Dim retval As Long ' return value

    ' Set the name of the new key and the default security settings
    subkey = "Software\ODBC\ODBC.INI"

    ' Create or open the registry key
    retval = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, hKey)
    If retval <> 0 Then
    Debug.Print "ERROR: Unable to open registry key!"
    Exit Sub
    End If

    ' Make room in the buffer to receive the incoming data.
    stringbuffer = Space(255)
    slength = 255
    ' Read the "username" value from the registry key.
    retval = RegQueryValueEx(hKey, "DataSourceName", 0, datatype, ByVal stringbuffer, slength)
    ' Only attempt to display the data if it is in fact a string.
    If datatype = REG_SZ Then
    ' Remove empty space from the buffer and display the result.
    stringbuffer = Left(stringbuffer, slength)
    Debug.Print "Database: "; stringbuffer
    Else
    ' Don't bother trying to read any other data types.
    Debug.Print "Data not in string format. Unable to interpret data."
    End If

    ' Close the registry key.
    retval = RegCloseKey(hKey)

    End Sub

    (Snagged from http://209.207.250.135/ref/r/regqueryvalueex.html)

  2. #2
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565

    Thumbs up

    Try this ...

    Code:
    Private Function DSNExist(DSNName As String) As Boolean
    '***************************************************************************
    'Purpose:     Determines whether a DSN already exists.
    'Parameters:  DSNName - DSN Name to be checked for.
    'Returns:     True/False - DSN Exists.
    '***************************************************************************
    Dim lngKeyHandle As Long
    Dim lngReturn As Long
    Dim strKey As String
      
      DSNExist = False
      
      strKey = "SOFTWARE\ODBC\ODBC.INI\" & DSNName
      
      ' Open registry key
      lngReturn = RegOpenKeyEx(HKEY_CURRENT_USER, strKey, 0, KEY_READ, lngKeyHandle)
      
      If lngReturn = ERROR_SUCCESS Then
        
        DSNExist = True
        
        ' Close registry key
        RegCloseKey lngKeyHandle
      
      End If
    
    End Function
    Put this in a code module ...

    Code:
    Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
                                                       (ByVal hKey As Long, _
                                                        ByVal lpSubKey As String, _
                                                        ByVal ulOptions As Long, _
                                                        ByVal samDesired As Long, _
                                                        phkResult As Long) As Long
    
    Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    
    ' RegOpenKey and RegCloseKey consts
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const ERROR_SUCCESS = 0&
    Public Const KEY_QUERY_VALUE = &H1
    Public Const KEY_NOTIFY = &H10
    Public Const READ_CONTROL = &H20000
    Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Public Const SYNCHRONIZE = &H100000
    Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Hope this helps

    [Edited by Stevie on 09-27-2000 at 09:01 AM]
    VB6 sp5, SQL Server 2000, C#

    There are no stupid questions. Only stupid people.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    2

    Thanks

    Looks simple - I will give it a try!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width