How to Set the Isolation Level in ODBC
- 1). Launch the programming language editor application by clicking on “Start.” Select “All Programs,” then select the application from the menu.
- 2). Start a new project by selecting -- under the “File” menu -- “New” and “Project.” You then would choose the required project type from the options. For Visual C++ or Visual Basic, you would choose “Windows Application.”
- 3). Type a name for your project and click “Save.”
- 1). Allocate the required handles. You first need to initialize the ODBC environment and allocate the environment handle before you can make any calls to elements of the ODBC API. You need to declare a variable of type “SQLHENV” or “SQLHANDLE” depending on your programming language, and pass it to the “SQLAllocHandle” function. According to MSDN, the correct syntax is:
SQLRETURN SQLAllocHandle(
SQLSMALLINT HandleType,
SQLHANDLE InputHandle,
SQLHANDLE * OutputHandlePtr);
For example, this is how you would implement it in Visual C++:
SQLHANDLE OdbcEnvironmentVariable;
SQLRETURN SQLReturnVariable;
SQLReturnVariable = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &OdbcEnvironmentVariable); - 2). Set the required environment attributes. You need to set the ODBC environment attribute using the SQLSetEnvAttr() function. This will tell the application which version of ODBC you are using. The syntax is:
SQLRETURN SQLSetEnvAttr(
SQLHENV EnvironmentHandle,
SQLINTEGER Attribute,
SQLPOINTER ValuePtr,
SQLINTEGER StringLength);
For example, to set the environment to ODBC 3.0 using Visual C++, you would type:
SQLReturnVariable = SQLSetEnvAttr(OdbcEnvironmentVariable, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER); - 3). Allocate the connection handle. You need to set up a connection handle to enable the application to connect to the database. You need to declare a variable of type “SQLHDBC” or “SQLHANDLE” and pass it to the “SQLAllocHandle” function with a “HANDLETYPE” of “SQL_HANDLE_DBC.” For example:
SQLHANDLE DBConnectionHandle;
SQLReturnVariable = SQLAllocHandle(SQL_HANDLE_DBC, OdbcEnvironmentVariable, &DBConnectionHandle) - 4). Establish a connection and set the connection attributes. You set the transaction isolation level at this point, using the “SQLSetConnectAttr” function. The syntax is:
SQLRETURN SQLSetConnectAttr(
SQLHDBC ConnectionHandle,
SQLINTEGER Attribute,
SQLPOINTER ValuePtr,
SQLINTEGER StringLength);
You set the “Attribute” to “SQL_ATTR_TXN_ISOLATION” and the “ValuePtr” to SQL_TXN_READ_UNCOMMITTED, SQL_TXN_READ_COMMITTED, SQL_TXN_REPEATABLE_READ or SQL_TXN_SERIALIZABLE, depending on the required transaction isolation level. For example:
SQLReturnVariable = SQLSetConnectAttr(DBConnectionHandle, SQL_ATTR_TXN_ISOLATION, SQL_TXN_READ_UNCOMMITTED, 0);