Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Get list of installed ODBC drivers using ODBC API.
[ All Languages » VB »  ODBC]

Total Hit ( 7762)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Here is a small code snippet to list all installed ODBC Drivers (name and attribute). On my machine I got the following output.
SQLServerUsageCount3:SQLLevel1:FileUsage0:DriverODBCVer02.50:ConnectFunctionsYYY:APILevel2:CPTimeout60MicrosoftAccessDriver.mdbUsageCount3:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage2:FileExtns.mdb:SQLLevel0MicrosoftTextDriver.txt;.csvUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns..asc.csv.tab.txt.csv:SQLLevel0MicrosoftExcelDriver.xlsUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.xls:SQLLevel0MicrosoftdBaseDriver.dbfUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.dbf.ndx.mdx:SQLLevel0MicrosoftParadoxDriver.dbUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.db:SQLLevel0MicrosoftVisualFoxProDriverUsageCount3:APILevel0:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.dbc.dbf:SQLLevel0MicrosoftFoxProVFPDriver.dbfUsageCount2:APILevel0:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.dbf.cdx.idx.fpt:SQLLevel0MicrosoftdBaseVFPDriver.dbfUsageCount2:APILevel0:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.dbf.cdx.idx.fpt:SQLLevel0MicrosoftAccessTreiber.mdbUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage2:FileExtns.mdb:SQLLevel0MicrosoftTextTreiber.txt;.csvUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns..asc.csv.tab.txt.csv:SQLLevel0MicrosoftExcelTreiber.xlsUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.xls:SQLLevel0MicrosoftdBaseTreiber.dbfUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.dbf.ndx.mdx:SQLLevel0MicrosoftParadoxTreiber.dbUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.db:SQLLevel0MicrosoftVisualFoxProTreiberUsageCount2:APILevel0:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.dbf.cdx.idx.fpt:SQLLevel0DriverdoMicrosoftAccess.mdbUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage2:FileExtns.mdb:SQLLevel0DriverdaMicrosoftparaarquivostexto.txt;.csvUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns..asc.csv.tab.txt.csv:SQLLevel0DriverdoMicrosoftExcel.xlsUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.xls:SQLLevel0DriverdoMicrosoftdBase.dbfUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.dbf.ndx.mdx:SQLLevel0DriverdoMicrosoftParadox.dbUsageCount2:APILevel1:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.db:SQLLevel0DriverparaoMicrosoftVisualFoxProUsageCount2:APILevel0:ConnectFunctionsYYN:DriverODBCVer02.50:FileUsage1:FileExtns.dbf.cdx.idx.fpt:SQLLevel0MicrosoftODBCforOracleUsageCount1:SQLLevel1:FileUsage0:DriverODBCVer02.50:ConnectFunctionsYYY:APILevel1:CpTimeout120OracleinOUIHomeAPILevel1:CPTimeout60:ConnectFunctionsYYY:DriverODBCVer03.51:FileUsage0:SQLLevel1
Step-By-Step Example

- Create a standard exe project
- Add the following code in form1

Click here to copy the following block
Const SQL_NULL_HANDLE = 0
Const SQL_HANDLE_ENV = 1
Const SQL_FETCH_NEXT = 1
Const SQL_FETCH_FIRST = 2
Const SQL_SUCCESS = 0
Const SQL_ATTR_ODBC_VERSION = 200
Const SQL_OV_ODBC2 = 2
Const SQL_IS_INTEGER = -6

Dim nRetCode As Long

Private Declare Function SQLDrivers Lib "odbc32.dll" (ByVal _
    EnvironmentHandle As Long, ByVal Direction As Integer, _
    ByVal DriverDescription As String, ByVal BufferLength1 As Integer, _
    DescriptionLengthPtr As Integer, ByVal DriverAttributes As String, _
    ByVal BufferLength2 As Integer, AttributesLengthPtr As Integer) As Integer

' Note that pointers to numbers are passed as numbers by reference!
Private Declare Function SQLDataSources Lib "odbc32.dll" (ByVal _
    EnvironmentHandle As Long, ByVal Direction As Integer, _
    ByVal ServerName As String, ByVal BufferLength1 As Integer, _
    NameLength1Ptr As Integer, ByVal Description As String, _
    ByVal BufferLength2 As Integer, NameLength2Ptr As Integer) As Integer


Private Declare Function SQLFreeHandle Lib "odbc32.dll" (ByVal _
    HandleType As Integer, ByVal Handle As Long) As Integer

Private Declare Function SQLAllocHandle Lib "odbc32.dll" (ByVal _
    HandleType As Integer, ByVal InputHandle As Long, _
    OutputHandlePtr As Long) As Integer

Private Declare Function SQLSetEnvAttr Lib "odbc32.dll" (ByVal _
    EnvironmentHandle As Long, ByVal EnvAttribute As Long, _
    ByVal ValuePtr As Long, ByVal StringLength As Long) As Integer


Private Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal _
    ConnectionHandle As Long) As Integer

'//This function will print list of installed driver name and attributes
Private Sub ListODBCDrivers()

  ' Prints a list of ODBC drivers on system

  Dim lHEnv       As Long
  Dim sDriverDesc    As String * 1024
  Dim sDriverAttr    As String * 1024
  Dim sDriverAttributes As String
  Dim nDriverDescLength As Integer
  Dim nAttrLength As Integer
  Dim x As Integer
  Dim sAll As String

  ' Allocate an environment handle.
  nRetCode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, lHEnv)

  ' Set ODBC behavior
  nRetCode = SQLSetEnvAttr(lHEnv, SQL_ATTR_ODBC_VERSION, _
      SQL_OV_ODBC2, SQL_IS_INTEGER)

  ' Get first driver
  nRetCode = SQLDrivers(lHEnv, _
      SQL_FETCH_FIRST, _
      sDriverDesc, _
      Len(sDriverDesc), _
      nDriverDescLength, _
      sDriverAttr, _
      Len(sDriverAttr), _
      nAttrLength)

  sAll = ""

  Me.WindowState = vbMaximized
  Me.AutoRedraw = True

  Do While nRetCode = SQL_SUCCESS

    '//Replace NULL separators with colons in attribute string
    sDriverAttributes = Left$(sDriverAttr, nAttrLength - 1)
    sDriverAttributes = Replace(sDriverAttributes, Chr(0), " : ")

    '//Remove NULL in ODBC Driver name
    sDriverDesc = Left$(sDriverDesc, nDriverDescLength)
    sDriverDesc = Replace(sDriverDesc, Chr(0), "")

    sAll = sAll & sDriverDesc & " ==> " & sDriverAttributes & vbCrLf

    Me.Print sDriverDesc                '//Print driver name
    Me.Print sDriverAttributes             '//Print driver attribute
    Me.Print String(100, "-")

    Debug.Print sDriverDesc              '//Print driver name
    Debug.Print sDriverAttributes           '//Print driver attribute
    Debug.Print String(100, "-")

    '//Next data source
    nRetCode = SQLDrivers(lHEnv, _
        SQL_FETCH_NEXT, _
        sDriverDesc, _
        Len(sDriverDesc), _
        nDriverDescLength, _
        sDriverAttr, _
        Len(sDriverAttr), _
        nAttrLength)
  Loop

  'Debug.Print "ODBC Drivers"
  'Debug.Print sAll

  nRetCode = SQLFreeHandle(SQL_HANDLE_ENV, lHEnv)
End Sub

Private Sub Form_Load()
  ListODBCDrivers
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.