TSqlError

Source: 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

DATATypeDescription
hErrEnvHandleODBC environment handle where the error occurred
hErrDbcHandleODBC connection handle where the error occurred
hErrStmtHandleODBC statement handle where the error occurred
cClassErrorCharacterSQL state class code (e.g., "42S02" for table not found)
nTypeNumericError type numeric code
cMsgErrorCharacterHuman-readable error message

Methods

MethodDescription
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

See Also