TDbOdbc / TDbOdbcDirect

Source: source/classes/tdbodbc.prg / source/classes/tdbodbcd.prg

TDbOdbc provides RDD-compatible ODBC access to SQL databases. It wraps ODBC statement handles and presents a DBF-like interface (GoTop, Skip, FieldGet, etc.), making it possible to browse SQL tables using the same navigation patterns as DBF files. TDbOdbcDirect is a lighter alternative that works directly with TDatabase-compatible ODBC connections without the full RDD emulation layer.

TDbOdbc - Key DATA Members

DATATypeDescription
hStmtHandleODBC statement handle
oOdbcObjectTODBC connection object
aFieldsArrayArray of field definition arrays
aFldPrimaryArrayPrimary key field names
aBufferArrayCurrent record buffer
aIndexArrayIndex/sort order definition
cSourceCharacterSource table name or SQL
cSelectCharacterGenerated SELECT statement
cOrderCharacterCurrent ORDER BY clause
cFilterCharacterCurrent WHERE filter
nRecnoNumericCurrent record number
nFieldsNumericNumber of fields
lBof / lEofLogicalBoundary indicators
lReadOnlyLogicalRead-only mode
lFoundLogicalResult of last Seek

TDbOdbc - Methods

MethodDescription
New( cSource, oOdbc, nArea, cAlias, lShared, lReadOnly )Create and open ODBC table access
End()Close statement handle and release resources
Execute( cCommand )Execute an SQL command directly
GoTop() / GoBottom()Navigate to first/last record
Skip( nRecs )Move relative by n records
GoTo( nRecNo )Go to a specific record by ID
FieldGet( nField )Get field value from current buffer
FieldPut( nField, uValue )Set field value (marks for update)
FieldName( nField )Get SQL column name
FieldType( nField )Get DBF-compatible field type
FieldLen( nField )Get field length
FieldDec( nField )Get field decimal places
FieldPos( cFieldName )Get field position by name
FCount()Number of fields
Seek( cExpression )Seek for expression in current order
SetOrder( cOrder )Set ORDER BY clause
SetFilter( cFilter )Set WHERE filter clause
Update( lForced )Write pending changes to database
Append()Add a new empty record
Delete()Delete current record
GenDbf( cFile, lWithData, cRdd )Generate a DBF file from the SQL result set
Refresh( lComplete )Re-fetch data from server

TDbOdbcDirect - Key DATA and Methods

MemberDescription
New( cSelect, oConnect )Create object with SELECT statement and TODBC connection
Open()Open the cursor and start fetching
Execute( cCommand )Execute SQL through the connection object
FieldGet( nField )Get field value via DBF alias
FieldName( nField )Get field name
FieldType( nField )Get field type
FieldLen( nField )Get field length
FieldPos( cField )Get field position by name
FCount()Number of fields
GoTop() / GoBottom() / Skip( nRecs )Navigation
RecNo() / GoTo( nRecNo )Position by record number
AddNew() / Edit() / Update()Record editing
Refresh()Re-fetch data from server
AutoFill( oWnd, nInterval, bEval )Auto-refresh data on a timer
End()Close cursor and release

Example: ODBC Table Browse

#include "FiveWin.ch"

function Main()

   local oOdbc, oTable, oWnd, oBrw

   oOdbc := TODBC():New( "MyDB" )  // ODBC data source name

   if oOdbc == nil
      MsgStop( "Cannot connect to ODBC source" )
      return nil
   endif

   oTable := TDbOdbc():New( "Employees", oOdbc )

   if oTable:lError
      MsgStop( "Cannot open table" )
      return nil
   endif

   DEFINE WINDOW oWnd TITLE "Employees from ODBC"

   @ 0, 0 XBROWSE oBrw OF oWnd AUTOSORT
   oBrw:SetRdd()
   oBrw:SetADO( oTable )     // link TDbOdbc to XBrowse
   oBrw:CreateFromCode()

   ACTIVATE WINDOW oWnd MAXIMIZED

   oTable:End()

return nil

Notes

See Also