TDbOdbc / TDbOdbcDirect
Fuente: 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
DATA Type Description
hStmtHandle ODBC statement handle
oOdbcObject TODBC connection object
aFieldsArray Array of field definition arrays
aFldPrimaryArray Primary key field names
aBufferArray Current record buffer
aIndexArray Index/sort order definition
cSourceCharacter Source table name or SQL
cSelectCharacter Generated SELECT statement
cOrderCharacter Current ORDER BY clause
cFilterCharacter Current WHERE filter
nRecnoNumeric Current record number
nFieldsNumeric Number of fields
lBof / lEofLogical Boundary indicators
lReadOnlyLogical Read-only mode
lFoundLogical Result of last Seek
TDbOdbc - Methods
Method Description
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
Member Description
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
TDbOdbc emulates the DBF RDD interface over ODBC, supporting navigation, editing, filtering, and sorting using SQL constructs transparently.
Primary keys are auto-detected via SQLPrimaryKeys() and used for UPDATE/DELETE statements.
The GenDbf() method allows exporting any ODBC result set to a DBF file for offline use.
TDbOdbcDirect is more lightweight and suitable for read-only or simple direct SQL access patterns without full RDD emulation.
Both classes work with the TODBC connection object for ODBC data source management.
Ver También ← tdbg | tdbodbcdirect →