|
|||||
ODBC connection information, options, and attributes | |||||
Introduction: ODBC connections |
Sample
The following code retrieves ODBC connection information. The first part allocates the environment and connection handles, then defines the streams used to retrieve the connection information.
local SQL_Handle_type EnvironmentHandle local SQL_Handle_type ConnectionHandle local SQL_Handle_type StatementHandle local SQL_Vector_type Vector local stream DataType local counter StringLen 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 SQLSetVectorSize( Vector, 1024 ) set RetCode to SQLGetInfo ( ConnectionHandle, SQL_DRIVER_ODBC_VER, Vector, 1024, StringLen ) output "Getting info - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" output "- Driver's odbc version = " || SQLGetVectorAsString(Vector) || "%n" done 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 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
The following code sets ODBC connection options. The first part allocates the environment and connection handles, then defines the streams that will contain the information used to set the connection options.
local SQL_Handle_type EnvironmentHandle local SQL_Handle_type ConnectionHandle local SQL_Vector_type ConAttr local counter CounterVal local counter RetCode SQLSetVectorSize( ConAttr, SQL_MAX_OPTION_STRING_LENGTH ) 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
The next part gets and sets the various ODBC connection options, then frees the connection and environment handles.
set RetCode to SQLGetConnectOption ( ConnectionHandle, SQL_OPT_TRACEFILE, ConAttr ) output "Getting connection option - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" output "- Trace file = " || SQLGetVectorAsString(ConAttr) || "%n" done SQLSetVectorAsString( ConAttr, "\NEWSQL.LOG" ) set RetCode to SQLSetConnectOption ( ConnectionHandle, SQL_OPT_TRACEFILE, ConAttr, SQL_CHAR ) output "Setting connection option - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" done set RetCode to SQLGetConnectOption ( ConnectionHandle, SQL_OPT_TRACEFILE, ConAttr ) output "Getting connection option - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" output "- Trace file = " || SQLGetVectorAsString(ConAttr) || "%n" done set RetCode to SQLGetConnectOption ( ConnectionHandle, SQL_ACCESS_MODE, ConAttr ) output "Getting connection option - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" set CounterVal to SQLGetVectorAsCounter( ConAttr ) output "- Access mode = %d(CounterVal)%n" done SQLSetVectorAsCounter( ConAttr, SQL_MODE_READ_ONLY ) set RetCode to SQLSetConnectOption ( ConnectionHandle, SQL_ACCESS_MODE, ConAttr, SQL_INTEGER ) output "Setting connection option - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" done set RetCode to SQLGetConnectOption ( ConnectionHandle, SQL_ACCESS_MODE, ConAttr ) output "Getting connection option - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" set CounterVal to SQLGetVectorAsCounter( ConAttr ) output "- Access mode = %d(CounterVal)%n" done 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
The following code sets ODBC connection attributes. The first part allocates the environment and connection handles, then defines the streams that will contain the information used to set the connection attributes.
local SQL_Handle_type EnvironmentHandle local SQL_Handle_type ConnectionHandle local SQL_Vector_type ConAttr local counter StringLen local counter CounterVal local counter RetCode SQLSetVectorSize( ConAttr, 100 ) 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
The following code gets and sets the various ODBC connection attributes, then frees the connection and environment handles.
set RetCode to SQLGetConnectAttr ( ConnectionHandle, SQL_ATTR_ACCESS_MODE, ConAttr, 100, StringLen ) output "Getting connection attribute - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" set CounterVal to SQLGetVectorAsCounter( ConAttr ) output "- Access mode = %d(CounterVal)%n" done SQLSetVectorAsCounter( ConAttr, SQL_MODE_READ_ONLY ) set RetCode to SQLSetConnectAttr ( ConnectionHandle, SQL_ATTR_ACCESS_MODE, ConAttr, 100, SQL_INTEGER ) output "Setting connection attribute - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" done set RetCode to SQLGetConnectAttr ( ConnectionHandle, SQL_ATTR_ACCESS_MODE, ConAttr, 100, StringLen ) output "Getting connection attribute - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" set CounterVal to SQLGetVectorAsCounter( ConAttr ) output "- Access mode = %d(CounterVal)%n" done set RetCode to SQLGetConnectAttr ( ConnectionHandle, SQL_ATTR_TRACEFILE, ConAttr, 100, StringLen ) output "Getting connection attribute - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" output "- Trace file = " || SQLGetVectorAsString(ConAttr) || "%n" done SQLSetVectorAsString( ConAttr, "\NEWSQL.LOG" ) set RetCode to SQLSetConnectAttr ( ConnectionHandle, SQL_ATTR_TRACEFILE, ConAttr, 100, SQL_CHAR ) output "Setting connection attribute - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" done set RetCode to SQLGetConnectAttr ( ConnectionHandle, SQL_ATTR_TRACEFILE, ConAttr, 100, StringLen ) output "Getting connection attribute - " do when RetCode != SQL_SUCCESS output "failed%n" halt with 1 else output "passed%n" output "- Trace file = " || SQLGetVectorAsString(ConAttr) || "%n" done 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
---- |