![]() ![]() ![]() ![]() ![]() ![]() ![]() |
|||||
|
|
|||||
| Connecting to an ODBC data source | |||||
| Introduction: ODBC connections |
Sample
When connecting to an ODBC data source, the first step is to allocate environment, connection, and statement handles.
local SQL_Handle_type EnvironmentHandle
local SQL_Handle_type StatementHandle
local SQL_Handle_type ConnectionHandle
local counter RetCode
set RetCode to SQLAllocEnv (EnvironmentHandle)
output "Allocating environment handle - "
do when RetCode != SQL_SUCCESS
output "failed%n"
halt with 1
else
output "passed%n"
done
set RetCode to SQLAllocHandle
(SQL_HANDLE_DBC, EnvironmentHandle, ConnectionHandle)
output "Allocating connection handle - "
do when RetCode != SQL_SUCCESS
output "failed%n"
halt with 1
else
output "passed%n"
done
set RetCode to SQLConnect (ConnectionHandle, "omodbc", 20, "", 0, "", 0)
output "Connecting to database - "
do when RetCode != SQL_SUCCESS
output "failed%n"
halt with 1
else
output "passed%n"
done
set RetCode to SQLAllocStmt (ConnectionHandle, StatementHandle)
output "Allocating statement handle - "
do when RetCode != SQL_SUCCESS
output "failed%n"
halt with 1
else
output "passed%n"
done
Next, you use SQLFreeStmt to free all resources associated with the statement handle.
set RetCode to SQLFreeStmt (StatementHandle, SQL_DROP)
output "Freeing statement handle resources - "
do when RetCode != SQL_SUCCESS
output "failed%n"
halt with 1
else
output "passed%n"
done
When you've finished with the data source, you tell the program to close the connection to the connection handle.
set RetCode to SQLDisconnect (ConnectionHandle)
output "Disconnecting from database - "
do when RetCode != SQL_SUCCESS
output "failed%n"
halt with 1
else
output "passed%n"
done
Finally, you use SQLFreeHandle again, this time to free all resources associated with the connection and environment handles.
set RetCode to SQLFreeHandle (SQL_HANDLE_DBC, ConnectionHandle)
output "Freeing connection handle resources - "
do when RetCode != SQL_SUCCESS
output "failed%n"
halt with 1
else
output "passed%n"
done
set RetCode to SQLFreeHandle (SQL_HANDLE_ENV, EnvironmentHandle)
output "Freeing environment handle resources - "
do when RetCode != SQL_SUCCESS
output "failed%n"
halt with 1
else
output "passed%n"
done
| ---- |