hi
i am trying to put image into database using SQLPutData.The function is returning SQL_SUCCESS.However we cannot retrieve image data using SQLGetData.These functions however work properly for normal character and integer datatypes.On using SQLGetData it returns SQL_ERROR and on using SQLDiagRec it returns SQL_NO_DATA.I don't know any other method of retrieving the data. Plz help me out!!!



BOOL flag=TRUE,mm;
SQLPOINTER Data[100];
TCHAR szName[255],buf[255];
SQLINTEGER cbName,rows=0;
DWORD cbTextSize,lbytes,BytesRead,FileSizeHigh,FileSize= 0;
SQLRETURN retcode;
SQLHSTMT hstmt;
SQLHDBC hdbc;
SQLHENV henv;
SQLCHAR Msg[255],SQLState[255];
SQLINTEGER MsgLen,NativeError;
PTR pParmID;
HANDLE hfile;
LPVOID lpMsgBuf[255];
//UCHAR Data[]="hope for the best\0";
hfile=CreateFile("\\\\.\\C:\\lk.txt",GENERIC_READ| GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if( hfile !=INVALID_HANDLE_VALUE )
{
FileSize = GetFileSize( (HANDLE) hfile, &FileSizeHigh );
if(FileSize > 0)
MessageBox(NULL,"size greater than 0","no problem",MB_OK);
MessageBox(NULL,"opening","noprob",MB_OK);

}
if(hfile==INVALID_HANDLE_VALUE)
{

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
}
if(FileSize==INVALID_FILE_SIZE)
{
MessageBox(NULL,"harsha","check",MB_OK);

}
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
SQLSetConnectAttr(hdbc, (void*)SQL_LOGIN_TIMEOUT, 5, 0);
retcode = SQLConnect(hdbc, (SQLCHAR *) "put", SQL_NTS,
(SQLCHAR*) "scott", SQL_NTS,(SQLCHAR *) "tiger",SQL_NTS);


retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
lbytes=165536;
cbTextSize=SQL_LEN_DATA_AT_EXEC(lbytes);
SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_C_BIN ARY,SQL_LONGVARBINARY,lbytes,0,(void *)1,0,&cbTextSize);
//lbytes=TEXTSIZE;
retcode = SQLExecDirect(hstmt,
"insert into puti values(?)",
SQL_NTS);
if(retcode==SQL_NEED_DATA)
MessageBox(NULL,"sofarsogood",NULL,MB_OK);
retcode = SQLParamData(hstmt, &pParmID);
lbytes=10;
if(retcode==SQL_NEED_DATA)
{

while(lbytes<FileSize)
{

mm=ReadFile(hfile,Data,lbytes, &BytesRead, NULL);
if(mm==0&&BytesRead==0&&flag==TRUE)
{
MessageBox(NULL,"read err","err",MB_OK);
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
break;
}
retcode=SQLPutData(hstmt,Data,lbytes);
MessageBox(NULL,Data,"success",MB_OK);
if(retcode==SQL_SUCCESS)
MessageBox(NULL,"success","success",MB_OK);
FileSize=FileSize-lbytes;
}
}

retcode = SQLParamData(hstmt, &pParmID);
SQLRowCount(hstmt,&rows);
itoa(rows,buf,10);
MessageBox(NULL,(char *)buf,"rows",MB_OK);
SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);



TCHAR szName[255];
//SQLBINARY szName[255];
SQLINTEGER cbName;
SQLRETURN retcode;
SQLHSTMT hstmt;
SQLHDBC hdbc;
SQLHENV henv;
SQLCHAR Msg[255],SQLState[255];
SQLINTEGER MsgLen,NativeError;
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
SQLSetConnectAttr(hdbc, (void*)SQL_LOGIN_TIMEOUT, 5, 0);
retcode = SQLConnect(hdbc, (SQLCHAR *) "m", SQL_NTS,
(SQLCHAR*) "scott", SQL_NTS,(SQLCHAR *) "tiger",SQL_NTS);
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
retcode = SQLExecDirect(hstmt,
"SELECT pic FROM puti",
SQL_NTS);
//if (retcode == SQL_ERROR)
//{
/* retcode=SQLGetDiagRec (SQL_HANDLE_STMT,hstmt,1,(SQLCHAR*)SQLState,&Nativ eError,Msg, 255, &MsgLen);
if(retcode==SQL_SUCCESS)
{
MessageBox(NULL,Msg,"Test",MB_OK);
MessageBox(NULL,SQLState,"error",MB_OK);
}
if(retcode==SQL_NO_DATA )
MessageBox(NULL,"diag not working",NULL,MB_OK);*/
//}

//if (retcode == SQL_SUCCESS) {
while (TRUE) {
retcode = SQLFetch(hstmt);
/*if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {
retcode=SQLGetDiagRec (SQL_HANDLE_STMT,hstmt,1,(SQLCHAR*)SQLState,&Nativ eError,Msg, sizeof(Msg), &MsgLen);
MessageBox(NULL,Msg,"fetchtest",MB_OK);
MessageBox(NULL,SQLState,"error",MB_OK);
break;
}*/
//if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){

/* Get data for columns 1, 2, and 3 */

//SQLGetData(hstmt, 1, SQL_C_ULONG, &sCustID, 0, &cbCustID);
SQLGetData(hstmt, 1, SQL_C_CHAR, szName, NAME_LEN, &cbName);
//SQLGetData(hstmt, 3, SQL_C_CHAR, szPhone, PHONE_LEN,
// &cbPhone);

/* Print the row of data */

/*fprintf(out, "%-5d %-*s %*s", sCustID, NAME_LEN-1, szName,
PHONE_LEN-1, szPhone);*/
MessageBox(NULL,szName,"SUCCESS",MB_OK);
}
// }
/*else
{
break;
}*/
// }
//}


SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);