#include #include #include #include int __cdecl wmain(int argc, _In_reads_(argc) WCHAR **argv) { SQLHENV hEnv = NULL; SQLHDBC hDbc = NULL; SQLHSTMT hStmt = NULL; SQLRETURN retcode; retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv); if (retcode == SQL_ERROR) return 1; // Register this as an application that expects 3.x behavior, // you must register something if you use AllocHandle retcode = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); if (retcode == SQL_ERROR) return 1; // Allocate a connection retcode = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc); if (retcode == SQL_ERROR) return 1; WCHAR *pwszConnStr = L"Driver={SQL Server};Server=192.168.178.34,56637;Uid=testuser;Pwd=testuser;Database=tempdb"; // Connect to the driver. Use the connection string if supplied // on the input, otherwise let the driver manager prompt for input. retcode = SQLDriverConnect(hDbc, GetDesktopWindow(), pwszConnStr, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE); if (retcode == SQL_ERROR) return 1; retcode = SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt); if (retcode == SQL_ERROR) return 1; SQLWCHAR* selectStatement = L"SELECT CAST(1 AS FLOAT) AS value"; retcode = SQLExecDirect(hStmt, selectStatement, SQL_NTS); if (retcode == SQL_ERROR) return 1; while (TRUE) { retcode = SQLFetch(hStmt); if (retcode == SQL_ERROR) break; if (retcode == SQL_NO_DATA) break; double val = 0; SQLLEN len = 0; retcode = SQLGetData(hStmt, 1, SQL_C_DOUBLE, (SQLPOINTER)&val, 0, &len); fwprintf(stdout, L"retcode: %d, retval: %f\n", retcode, val); } SQLFreeHandle(SQL_HANDLE_STMT, hStmt); retcode = SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt); if (retcode == SQL_ERROR) return 1; retcode = SQLExecDirect(hStmt, selectStatement, SQL_NTS); if (retcode == SQL_ERROR) return 1; while (TRUE) { retcode = SQLFetch(hStmt); if (retcode == SQL_ERROR) break; if (retcode == SQL_NO_DATA) break; TCHAR buf[64]; SQLLEN len = 0; retcode = SQLGetData(hStmt, 1, SQL_C_TCHAR, (SQLPOINTER)&buf, sizeof(buf) / sizeof(TCHAR), &len); fwprintf(stdout, L"retcode: %d, retval: %s\n", retcode, buf); } SQLFreeHandle(SQL_HANDLE_STMT, hStmt); SQLFreeHandle(SQL_HANDLE_DBC, hDbc); return 0; }