Listing Available DSN / Drivers Installed

This code shows how to list the available DSN and Drivers installed on the computer.

screen-shot

The program works by utilizing the SQLDataSources API of the ODBC32.DLL.



private Declare Function SQLDataSources Lib "ODBC32.DLL" _
   (byval henv&, byval fDirection%, byval szDSN$, byval cbDSNMax%, _
   pcbDSN%, byval szDescription$, byval cbDescriptionMax%, _
   pcbDescription%) as Integer
'
private Declare Function SQLAllocEnv% Lib "ODBC32.DLL" (env&)
'
Const SQL_SUCCESS as Long = 0
Const SQL_FETCH_NEXT as Long = 1
'
Sub GetDSNsAndDrivers()
    Dim i as Integer
    Dim sDSNItem as string * 1024
    Dim sDRVItem as string * 1024
    Dim sDSN as string
    Dim sDRV as string
    Dim iDSNLen as Integer
    Dim iDRVLen as Integer
    Dim lHenv as Long         'handle to the environment

    on error resume next
    cboDSNList.AddItem "(None)"

    'get the DSNs
    If SQLAllocEnv(lHenv) <> -1 then
        Do Until i <> SQL_SUCCESS
            sDSNItem = Space$(1024)
            sDRVItem = Space$(1024)
            i = SQLDataSources(lHenv, SQL_FETCH_NEXT, sDSNItem, 1024, _
                iDSNLen, sDRVItem, 1024, iDRVLen)
            sDSN = Left$(sDSNItem, iDSNLen)
            sDRV = Left$(sDRVItem, iDRVLen)
                
            If sDSN <> Space(iDSNLen) then
                cboDSNList.AddItem sDSN
                cboDrivers.AddItem sDRV   '---optional - driver
                                          '--- value returned
            End If
        Loop
    End If
    'remove the dups
    If cboDSNList.ListCount > 0 then
        With cboDrivers
            If .ListCount > 1 then
                i = 0
                While i < .ListCount
                    If .List(i) = .List(i + 1) then
                        .RemoveItem (i)
                    else
                        i = i + 1
                    End If
                Wend
            End If
        End With
    End If
    cboDSNList.ListIndex = 0
End Sub
'

Download Demo Project (2k)



Comments

  • Wonderful

    Posted by Legacy on 12/23/2003 12:00am

    Originally posted by: Rosa

    it works at the first time! I would like also know how to get the odbc list when you select the dsn. I will invesigate.

    Reply
  • Useful one

    Posted by Legacy on 11/22/2003 12:00am

    Originally posted by: Rakesh Sharma

    This is the utility really needed to trace a computer (which is not yours), when you are supposed to work on it.
    This will tell you all the things in use and you just may use them.

    Reply
  • Worked perfect

    Posted by Legacy on 05/01/2003 12:00am

    Originally posted by: Greg Dirst

    Thanks

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds