|        | |||||
|  | |||||
| Retrieving ODBC driver and data source information | |||||
| Introduction: ODBC connections | 
Sample
When retrieving ODBC driver and data source information, you must allocate the environment handle, then identify the stream and counter shelves that will store this information.
  local SQL_Handle_type EnvironmentHandle
  local counter RetCode
  local stream String1
  local stream String2
  local counter StringLen1
  local counter StringLen2
  local counter Direction
  set RetCode to SQLAllocEnv(EnvironmentHandle)
  output "Allocating environment handle - "
  do when RetCode != SQL_SUCCESS
     output "failed%n"
    halt with 1
  else
     output "passed%n"
  done
The ODBC functions SQL_FETCH_FIRST and SQL_FETCH_NEXT are used by SQLDrivers to fetch the "first" (the search starts from the beginning of the list) or "next" driver description in the list.
  set Direction to SQL_FETCH_FIRST
  output "Retrieving driver information - "
  repeat
     set RetCode to SQLDrivers
        (  EnvironmentHandle,
           Direction,
           String1,
           1024,
           StringLen1,
           String2,
           1024,
           StringLen2
        )
     do when RetCode != SQL_SUCCESS
        output "failed%n" when Direction=SQL_FETCH_FIRST
        exit
     else
        do when Direction=SQL_FETCH_FIRST
           output "passed%n"
           set Direction to SQL_FETCH_NEXT
        done
        output "- %g(String1)(%d(StringLen1)) => (%d(StringLen2))%n"
     done
  again
The ODBC functions SQL_FETCH_FIRST and SQL_FETCH_NEXT are used by SQLDataSources to return information about the data source.
  set Direction to SQL_FETCH_FIRST
  output "Retrieving data source information - "
  repeat
     set RetCode to SQLDataSources
        (  EnvironmentHandle,
           Direction,
           String1,
           1024,
           StringLen1,
           String2,
           1024,
           StringLen2
        )
     do when RetCode != SQL_SUCCESS
        output "failed%n" when Direction=SQL_FETCH_FIRST
        exit
     else
        do when Direction=SQL_FETCH_FIRST
           output "passed%n"
           set Direction to SQL_FETCH_NEXT
        done
        output "- %g(String1)(%d(StringLen1)) => (%d(StringLen2))%n"
     done
  again
When you've finished retrieving driver and data source information, remember to free environment handle resources.
  set RetCode to SQLFreeHandle(SQL_HANDLE_ENV, EnvironmentHandle)
  output "Freeing environment handle resources - "
  do when RetCode != SQL_SUCCESS
     output "failed%n"
     halt with 1
  else
     output "passed%n"
  done
| ---- |