#include #include #include #include #include #include #include #include #include SQLCHAR sqlstate[25]; SQLCHAR message[255]; SQLINTEGER native_error; int main() { std::string s1; SQLHENV henv = SQL_NULL_HENV; SQLHDBC hdbc; SQLHSTMT hstmt = SQL_NULL_HSTMT; RETCODE rc = SQL_SUCCESS; char szDr[64]; std::string strDriver="DSN=PostgreSQL35W"; strcpy_s(szDr,strDriver.c_str()); char szCnOut[512]; SWORD swStrLen; printf("Please Enter key...\n"); getchar(); rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); if (rc != SQL_ERROR) { printf("SQLAllocHandle() OK\n"); if(!SQL_SUCCEEDED(SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER))) printf("SQLSetEnvAttr/SQL_ATTR_ODBC_VERSION error\n"); rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc); printf("SQLAllocHandle() ok\n"); rc = SQLDriverConnect(hdbc,NULL,(SQLTCHAR*)szDr,(SQLSMALLINT)strlen(szDr),(SQLTCHAR*)szCnOut,512,&swStrLen,SQL_DRIVER_NOPROMPT); if (rc != SQL_SUCCESS){ printf("connect failed\n"); } printf("SQLConnect ok\n"); SQLLEN length; char data[24]; //data buffer LPSTR sql = "SELECT now()::timestamp(3)"; SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); rc = SQLPrepare(hstmt,(SQLCHAR *)sql,SQL_NTS); rc = SQLBindCol(hstmt,1,SQL_C_CHAR,data,sizeof(data),&length); rc = SQLExecute(hstmt); if(rc == SQL_SUCCESS) { printf("data-length=%d\n",sizeof(data)); if(rc != SQL_SUCCESS) { printf("SQLBindCol() failed\n"); } for(int i=0; ; i++) { rc = SQLFetch(hstmt); if(rc == SQL_NO_DATA) { printf("no data\n"); break; } if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { printf("SQLFetch() failed\n"); break; } if(rc == SQL_SUCCESS_WITH_INFO) { SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, 1, sqlstate, &native_error, message, 255, nullptr); printf("SQLSTATE=%s MESSAGE=%s\n",sqlstate,message); } printf("length=%d\n",length); printf("%s\n",data); //display data. } } ::SQLCloseCursor(hstmt); rc = SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC,hdbc); SQLFreeHandle(SQL_HANDLE_ENV,henv); } return 0; }