Hi,
I am trying to store a file in a database in an 'image' column. The code I am
using (shown below) works fine for files that are below 8002 bytes in size but
any larger files fail, once more than 8001 bytes have been sent using the
SQLPutData command. It doesn't matter what size of chunks of the file I send to
the Database but once I have sent over about 8000 bytes it fails.
I am using SQLServer 2000
ODBC version 2000.80.528.00
Windows 2000
Thanks very much for any help, it is much appreciated
James.
/**********CODE BELOW************
********************************/
// Bind the data parameter
SDWORD cbValue = SQL_DATA_AT_EXEC;//SQL_LEN_DATA_AT_EXEC(Length);
retcode = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
SQL_VARBINARY, Length,0, NULL, 0, &cbValue );
// Check for an error
if (retcode != SQL_SUCCESS && retcode != SQL_NEED_DATA)
AfxThrowDBException( retcode, &theDatabase, stmt );
// Set the message text.
pMessage->SetWindowText ( "Adding file " + ActualFileName + " to database" );
Statement.Format("INSERT INTO SystemFiles ( Directory, FileName, Length, Data)
VALUES ('%s', '%s', %d, ?)",Directory, ActualFileName, Length);
retcode = SQLExecDirect( stmt, (unsigned char *)Statement.GetBuffer(256),
SQL_NTS );
Statement.ReleaseBuffer();
// Check for an error
if (retcode != SQL_SUCCESS && retcode != SQL_NEED_DATA)
AfxThrowDBException( retcode, &theDatabase, stmt );
// Now send the data
if (retcode == SQL_NEED_DATA) {
DWORD dummy;
retcode = SQLParamData(stmt,(void **)&dummy);
if (retcode != SQL_SUCCESS && retcode != SQL_NEED_DATA)
AfxThrowDBException( retcode, &theDatabase, stmt );
char Buffer[8001]; // (SQLserver max 8001 ie. 8002 fails)
DWORD Done;
for (;;)
{
ReadFile(hFile, Buffer, sizeof(Buffer), &Done, NULL);
retcode = SQLPutData(stmt, Buffer, Done);
if (retcode != SQL_SUCCESS && retcode != SQL_NEED_DATA)
AfxThrowDBException( retcode, &theDatabase, stmt );
// Update the progress bar...
LengthSoFar += Done;
pProgress->SetPos((int)((LengthSoFar * 100) / TotalLength));
// Have we finished?
if (Done < sizeof(Buffer))
break;
// Do we stop?
if (Stop)
break;
}
// Signal we have finished
pMessage->SetWindowText ( "Committing "+ActualFileName );
SQLParamData(stmt, (void **)&dummy);
// Free the statementQuote:}
SQLFreeStmt( stmt, SQL_DROP );
// Free the file handle
CloseHandle(hFile);