#define CONNSTR "DSN=WAD;" #define SAMPLE_QUERY "SELECT @@VERSION" #include #include #include #include #define Test(x) check_return_value(x, #x) SQLRETURN check_return_value(SQLRETURN value, char *command) { printf("] %s: ", command); switch (value) { case SQL_SUCCESS: printf("SQL_SUCCESS\n"); break; case SQL_SUCCESS_WITH_INFO: printf("SQL_SUCCESS_WITH_INFO\n"); break; case SQL_NO_DATA: printf("SQL_NO_DATA\n"); break; case SQL_ERROR: printf("SQL_ERROR\n"); break; case SQL_INVALID_HANDLE: printf("SQL_INVALID_HANDLE\n"); break; case SQL_STILL_EXECUTING: printf("SQL_STILL_EXECUTING\n"); break; case SQL_NEED_DATA: printf("SQL_NEED_DATA\n"); break; default: printf("Unknown (%d)\n", value); break; } return value; } HWND create_dummy_hwnd() { HWND ret = CreateWindow("STATIC", "Dummy Window", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_MESSAGE, NULL, NULL, NULL); if ((ret == NULL) || (ret == INVALID_HANDLE_VALUE)) printf("Failed to create dummy window for SQLDriverConnect\n\n"); else printf("Dummy window handle: 0x%08X\n\n", ret); return ret; } int main() { HENV hEnv; HDBC hDbc; HSTMT hStmt; HWND hWnd = create_dummy_hwnd(); SQLCHAR OutConnectionString[500]; SQLSMALLINT BufferLength = sizeof(OutConnectionString); SQLINTEGER IsDead; #define pSQL_OV_ODBC3 ((SQLPOINTER)SQL_OV_ODBC3) Test(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv)); Test(SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, pSQL_OV_ODBC3, 0)); Test(SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc)); Test(SQLDriverConnect(hDbc, hWnd, CONNSTR, sizeof(CONNSTR), OutConnectionString, BufferLength, &BufferLength, SQL_DRIVER_COMPLETE_REQUIRED)); Test(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt)); printf("\n" "Now, please shut down the database server, or disconnect from the network, or\n" "otherwise make the database connection become unavailable. When you are\n" "certain the server is no longer reachable, press enter.\n"); scanf("%*c"); Test(SQLExecDirect(hStmt, SAMPLE_QUERY, sizeof(SAMPLE_QUERY))); printf("\n" "^-- If the above returned SQL_SUCCESS, then the test will need to be re-run,\n" "as the connection is either still available, or its loss has not been\n" "detected by the ODBC driver.\n" "\n" "If it returned an error as expected, then the following call should indicate\n" "that the ODBC driver is aware that the connection is lost:\n" "\n"); BufferLength = sizeof(IsDead); IsDead = 0xDEAD; Test(SQLGetConnectAttr(hDbc, SQL_ATTR_CONNECTION_DEAD, &IsDead, BufferLength, &BufferLength)); printf("\n" "The value returned by SQLGetConnectAttr is: IsDead == %d\n", IsDead); if (IsDead == 0xDEAD) printf("\nThe call to SQLGetConnectAttr did not assign a value to the input buffer.\n\n"); else if (IsDead == 0) printf("\nThe ODBC driver is not indicating that the connection was dropped.\n\n"); else printf("\nThe ODBC driver is correctly reporting that the connection was dropped.\n"); return 0; }