This example uses only Core commands to establish a connection to the data source and create a table. Next, this application prepares an INSERT statement, sets the parameters for the insertion, and executes the statement. Then, the application directly executes a SELECT statement, binds the first two columns of data to ESL variables, and uses a loop to fetch the data and print it to an error log file. Finally, it disconnects from the data source and frees the statement, connection, and environment handles.
include eds.inc
##
## Declare stimulus for EDS.DLL.
##
stimulus EDSStimulus "eds"
##
## Declare variables.
##
string Dsn_SV is "default"
string Uid_SV is " YourUserID"
string Pwd_SV is " YourPassword"
string UseDsn_SV is "USE TESTDB"
string DeleteTable_SV is "DROP TABLE EMP"
string CreateTable_SV is "CREATE TABLE EMP (ID INT, NAME VARCHAR(80))"
string InsertString_SV is "INSERT INTO EMP VALUES (?, ?)"
string SelectString_SV is "SELECT ID, NAME FROM EMP"
string SQLState_SV
string ErrorText_SV
string ResultName_SV
string Name_SV
integer Henv_IV
integer Hdbc_IV
integer Hstmt_IV
integer Error_IV
integer NativeError_IV
integer Id_IV
integer ValueLen_IV
integer ParamNum_IV
integer ColumnNum_IV
integer Row_IV
integer ResultId_IV
integer Maxbytes_IV
integer Numbytes_IV
boolean Forever_BV is true
##
## Declare insert data arrays.
##
string Name[5] is "Gary","Mary","Lori","Jane","Mike"
integer Id[5] is 7139,7600,4040,4994,2929
##
## Declare structure for Static Bind.
##
structure EDSCOLDEF_STRUCT Colinfo_Struct
action CheckForError is
##
## Call EDSQueryInternalError to process
## internal EDS error.
##
if (Error_IV < EDS_INTERNAL_ERR_START) then
call EDSQueryInternalError(Error_IV,
ErrorText_SV)
send ErrorText_SV "\n" to errorlog
exit
end if
##
## Call EDSError to process ODBCS error. ##
if ((Error_IV > EDS_INTERNAL_ERR_START) and
( Error_IV < EDS_SUCCESS)) then
call EDSError( Henv_IV,
Hdbc_IV,
Hstmt_IV,
SQLState_SV,
NativeError_IV,
ErrorText_SV)
send "SQL State : " SQLState_SV "\nError Text: " ErrorText_SV "\n" to errorlog
exit
end if
response to start
##
## Allocate environment handle.
##
call EDSAllocEnv(Henv_IV)
copy errorlevel to Error_IV
action CheckForError
##
## Allocate connection handle.
##
call EDSAllocConnect(Henv_IV,
Hdbc_IV)
copy errorlevel to Error_IV
action CheckForError
##
## Connect to a data source.
##
call EDSConnect(Hdbc_IV,
Dsn_SV,
Uid_SV,
Pwd_SV)
copy errorlevel to Error_IV
action CheckForError
##
## Allocate a statement handle.
##
call EDSAllocStmt(Hdbc_IV,
Hstmt_IV)
copy errorlevel to Error_IV
action CheckForError
##
## Execute SQL: Use data source TestDB.
##
call EDSExecDirect(Hstmt_IV,
UseDsn_SV)
copy errorlevel to Error_IV
action CheckForError
##
## Execute SQL: Delete the previous creation of
## sample table.
##
call EDSExecDirect(Hstmt_IV,
DeleteTable_SV)
copy errorlevel to Error_IV
action CheckForError
##
## Execute SQL: Create the new table.
##
call EDSExecDirect(Hstmt_IV,
CreateTable_SV)
copy errorlevel to Error_IV
action CheckForError
##
## Prepare the INSERT statement.
##
call EDSPrepare(Hstmt_IV,
InsertString_SV)
copy errorlevel to Error_IV
action CheckForError
##
## Set the insert parameters.
##
copy 1 to ParamNum_IV
copy 1 to ColumnNum_IV
copy 0 to ValueLen_IV
copy EDSSetParam(Hstmt_IV,
ParamNum_IV,
EDS_INT,
EDS_SQL_INTEGER,
0,
0,
Id_IV,
ValueLen_IV) to Error_IV
action CheckForError
copy 2 to ParamNum_IV
copy 2 to ColumnNum_IV
copy 80 to ValueLen_IV
copy EDSSetParam(Hstmt_IV,
ParamNum_IV,
EDS_STRING,
EDS_SQL_VARCHAR,
80,
0,
Name_SV,
ValueLen_IV) to Error_IV
action CheckForError
##
## Execute the INSERT.
##
for Row_IV = 1 to 5 loop
copy Id[ Row_IV] to Id_IV
copy Name[ Row_IV] to Name_SV
call EDSExecute(Hstmt_IV)
copy errorlevel to Error_IV
action CheckForError
end loop
##
## Execute the selection.
##
call EDSExecDirect(Hstmt_IV,
SelectString_SV)
copy errorlevel to Error_IV
action CheckForError
##
## Bind and fetch the result set.
##
copy 1 to ColumnNum_IV
copy 0 to Maxbytes_IV
copy EDSBindCol(Hstmt_IV,
ColumnNum_IV,
EDS_INT,
ResultId_IV,
Maxbytes_IV,
Numbytes_IV) to Error_IV
action CheckForError
copy 2 to ColumnNum_IV
copy 80 to Maxbytes_IV
copy EDSBindCol(Hstmt_IV,
ColumnNum_IV,
EDS_STRING,
ResultName_SV,
Maxbytes_IV,
Numbytes_IV) to Error_IV
action CheckForError
##
## Print a header to the errorlog first.
##
send "ID NAME\n____________\n" to errorlog
##
## Fetch results; write them to errorlog.
##
while (Forever_BV) loop
call EDSFetch(Hstmt_IV)
copy errorlevel to Error_IV
action CheckForError
if ((Error_IV = EDS_NO_DATA_FOUND) or
(Error_IV = EDS_ERROR)) then
leave loop
else
send ResultId_IV " " ResultName_SV "\n" to errorlog
end if
end loop
##
## Free the statement handle.
##
copy EDSFreeStmt(Hstmt_IV,
EDS_DROP) to Error_IV
action CheckForError
##
## Disconnect from data source.
##
copy EDSDisconnect(Hdbc_IV) to Error_IV
action CheckForError
##
## Free the connection handle.
##
copy EDSFreeConnect(Hdbc_IV) to Error_IV
action CheckForError
##
## Free the environment handle.
##
copy EDSFreeEnv(Henv_IV) to Error_IV
action CheckForError
##
## Exit the application
##
exit