TSqlError
Fuente: source/classes/sqlerror.prg
Standalone class
TSqlError encapsulates ODBC SQL error information. It captures error details at the environment, connection, and statement handle levels, providing structured access to native SQL state codes, error messages, and error types. It is used internally by the TODBC class and can be used directly for custom error handling.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
hErrEnv | Handle | ODBC environment handle where the error occurred |
hErrDbc | Handle | ODBC connection handle where the error occurred |
hErrStmt | Handle | ODBC statement handle where the error occurred |
cClassError | Character | SQL state class code (e.g., "42S02" for table not found) |
nType | Numeric | Error type numeric code |
cMsgError | Character | Human-readable error message |
Methods
| Method | Description |
|---|---|
New( hEnv, hDbc ) | Create a new TSqlError object; captures errors from environment and connection handles |
Init() | Initialize the error object and retrieve diagnostic records |
GenerateError( hStmt ) | Capture error details from a statement handle after a failed SQL operation |
CreateError( cClass, nType, cMsg ) | Create a synthetic error with explicit class, type, and message |
ClassError() | Return the SQL state class code |
Type() | Return the error type numeric code |
MsgError() | Return the human-readable error message |
Example: Detect and Display SQL Error
#include "FiveWin.ch"
function Main()
local oOdbc, oErr
oOdbc := TODBC():New( "MyDSN", "user", "pass" )
if ! oOdbc:lSuccess
// Create and display the error
oErr := TSqlError():New( oOdbc:hEnv, oOdbc:hDbc )
oErr:Init()
? "SQL Error:"
? " Class:", oErr:ClassError()
? " Type:", oErr:Type()
? " Message:", oErr:MsgError()
MsgStop( oErr:MsgError(), "ODBC Error: " + oErr:ClassError() )
endif
return nil
Notes
- TSqlError uses
SQLError()/SQLGetDiagRec()ODBC API calls to retrieve diagnostic information from the various handles involved in a failed operation. - The
cClassErrorcontains the standard SQLSTATE code (5-character string) defined by the ODBC and SQL standards. GenerateError( hStmt )should be called immediately after the SQL statement fails, before any other ODBC calls that might overwrite the diagnostic information.- Use
CreateError()to raise custom TSqlError objects with application-defined error messages for non-ODBC error scenarios.