hi all,
i'm new to odbc programming in c++. i've put together a simple program to learn on and it's failing in two places. first, a string value is coming back as jibberish. also, rowcount returns a negative value. what am i doing wrong here? connecting to a sql server database.
i'm new to odbc programming in c++. i've put together a simple program to learn on and it's failing in two places. first, a string value is coming back as jibberish. also, rowcount returns a negative value. what am i doing wrong here? connecting to a sql server database.
Code:
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
RETCODE retcode;
SQLHENV hENVIR;
SQLHDBC hCONNECT;
SQLHSTMT hSTMT;
SQLCHAR szServer[] = "sql76";
SQLCHAR szUser[] = "user";
SQLCHAR szPassword[] = "password";
SQLCHAR szSQL[256];
//CORE INITIALIZATION
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hENVIR);
SQLSetEnvAttr(hENVIR, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, hENVIR, &hCONNECT);
retcode = SQLConnect(hCONNECT, szServer, SQL_NTS, szUser, SQL_NTS, szPassword, SQL_NTS);
SQLAllocHandle(SQL_HANDLE_STMT, hCONNECT, &hSTMT );
//DATA COMMUNICATION
SQLINTEGER sqPTID;
SQLCHAR sqPATFNAME[256];
SQLINTEGER signal;
SQLBindCol(hSTMT, 1, SQL_C_SLONG, &sqPTID, sizeof(sqPTID), &signal);
SQLBindCol(hSTMT, 2, SQL_C_CHAR, &sqPATFNAME, SQL_NTS, &signal);
long state;
strcpy((char*)szSQL,"SELECT PTID, PAT_FIRST_NAME FROM mrpa99");
state = SQLExecDirect(hSTMT,szSQL, SQL_NTS);
state = SQLFetch(hSTMT);
state = SQLFetch(hSTMT);
//sqPATFNAME printing as a series of ¦¦¦¦¦¦¦ characters
cout << sqPTID << ": " << sqPATFNAME << endl;
SQLINTEGER rowcount;
SQLRowCount(hSTMT, &rowcount);
//rowcount returning -1
cout << rowcount << endl;
SQLCloseCursor(hSTMT);
SQLEndTran (SQL_HANDLE_ENV, hENVIR, SQL_ROLLBACK);
//CORE CLEANUP
SQLFreeHandle(SQL_HANDLE_STMT, hSTMT);
SQLDisconnect(hCONNECT);
SQLFreeHandle(SQL_HANDLE_DBC, hCONNECT);
SQLFreeHandle(SQL_HANDLE_ENV, hENVIR);
return 0;
}