--- title: "Asynchronous Execution (Notification Method) Sample | Microsoft Docs" ms.custom: "" ms.date: "01/19/2017" ms.prod: sql ms.prod_service: connectivity ms.reviewer: "" ms.technology: connectivity ms.topic: conceptual ms.assetid: 55c51fff-119d-445f-8732-c1569966e559 author: David-Engel ms.author: v-daenge --- # Asynchronous Execution (Notification Method) Sample [!INCLUDE[Driver_ODBC_Download](../../../includes/driver_odbc_download.md)] The code samples in this topic demonstrate how to use the [Asynchronous Execution (Notification Method)](https://msdn.microsoft.com/library/hh405038(VS.85).aspx). This function uses asynchronous notification to open five connections, and executes one query on a statement of each connection. ``` #define NUMBER_OPERATIONS 5 int AsyncNotificationSample(void) { RETCODE rc; SQLHENV hEnv = NULL; SQLHDBC arhDbc[NUMBER_OPERATIONS] = {NULL}; SQLHSTMT arhStmt[NUMBER_OPERATIONS] = {NULL}; HANDLE arhDBCEvent[NUMBER_OPERATIONS] = {NULL}; RETCODE arrcDBC[NUMBER_OPERATIONS] = {0}; HANDLE arhSTMTEvent[NUMBER_OPERATIONS] = {NULL}; RETCODE arrcSTMT[NUMBER_OPERATIONS] = {0}; rc = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &hEnv); if ( !SQL_SUCCEEDED(rc) ) goto Cleanup; rc = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3_80, SQL_IS_INTEGER); if ( !SQL_SUCCEEDED(rc) ) goto Cleanup; // Connection operations begin here // Alloc NUMBER_OPERATIONS connection handles for (int i=0; i(Context); SQLRETURN rc ; SQLCompleteAsync(SQL_HANDLE_DBC, g_hDbcs[(int)i], &rc); printf("Connection %d done: RC: %d, threadid:%u \n", (int)i, rc, GetCurrentThreadId()); InterlockedIncrement(&g_JobDoneNumber); } int _tmain(int argc, _TCHAR* argv[]) { for(int i = 0; i< g_nConnection; i++) g_hevents[i] = CreateEvent(NULL, FALSE, FALSE, NULL); PTP_WAIT waits[g_nConnection]; for(int i = 0; i < g_nConnection; i++) { waits[i] = CreateThreadpoolWait(&WaitCallBack, reinterpret_cast((UINT_PTR)i), NULL); SetThreadpoolWait(waits[i], g_hevents[i], NULL); } SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,&g_hEnv); SQLSetEnvAttr(g_hEnv,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3_80, SQL_IS_UINTEGER); for(int i = 0; i < g_nConnection; i++) { SQLAllocHandle( SQL_HANDLE_DBC, g_hEnv , &g_hDbcs[i]); SQLSetConnectAttr( g_hDbcs[i], SQL_ATTR_ASYNC_DBC_FUNCTIONS_ENABLE, (SQLPOINTER)SQL_ASYNC_ENABLE_ON, SQL_IS_INTEGER); SQLSetConnectAttr(g_hDbcs[i], SQL_ATTR_ASYNC_DBC_EVENT, g_hevents[i], SQL_IS_POINTER); } // make connections g_JobDoneNumber = 0; for(int i = 0; i < g_nConnection; i++) { SQLDriverConnect(g_hDbcs[i],NULL, (SQLCHAR*)"DRIVER={ODBC Driver 13 for SQL Server};Server=your_server;database=your_database;uid=usr;pwd=your_password", SQL_NTS, (SQLCHAR*)g_connOut[i].szOutConnectionString, 500, &g_connOut[i].iLen, SQL_DRIVER_NOPROMPT); } printf("connect wait..\n"); while(g_JobDoneNumber < g_nConnection) SleepEx(50, false); // disconnect for(int i = 0; i < g_nConnection; i++) SetThreadpoolWait(waits[i], g_hevents[i], NULL); printf("disconnect wait..\n"); g_JobDoneNumber = 0; for(int i = 0; i < g_nConnection; i++) SQLDisconnect(g_hDbcs[i]); while(g_JobDoneNumber < g_nConnection) SleepEx(50, false); for(int i = 0; i < g_nConnection; i++) CloseThreadpoolWait(waits[i]); for(int i = 0; i < g_nConnection; i++) { CloseHandle(g_hevents[i]); SQLFreeHandle(SQL_HANDLE_DBC, g_hDbcs[i]); } SQLFreeHandle(SQL_HANDLE_ENV, g_hEnv); return 0; } ``` ## See Also [Microsoft ODBC Driver for SQL Server on Windows](../../../connect/odbc/windows/microsoft-odbc-driver-for-sql-server-on-windows.md)