PRB: CLongBinary Data Doesn't Update with CRecordset::Open appendOnly Option

ID: Q234797


The information in this article applies to:


SYMPTOMS

When you call CRecordset::Open with the appendOnly option, you can't update any data that is in CLongBinary member variables. The Update call does not cause any errors, but the data in that field is not written to the database.


CAUSE

The CLongBinary member variable is not being bound to the table's column in the RFX_LongBinary function. RFX_LongBinary only binds the column if the recordset is updateable [CanUpdate() returns TRUE], but when opened with the appendOnly option, the recordset is only appendable and not updateable.

NOTE: If a driver does not support SQL_GD_BOUND for the SQL_GETDATA_EXTENSIONS value of SQLGetInfo(), MFC will attempt to perform SQL updates rather than positioned updates using SQLSetPos(), and therefore you will not see this problem. For example, if you are using a SQL Server datasource, you will not see a problem.


RESOLUTION

Use one of the following three workarounds:


MORE INFORMATION

Steps to Reproduce the Problem

  1. Use the AppWizard to create a new MFC application that contains a CRecordset-derived class that is based on a table that contains a long binary column (such as an OLE object field in Microsoft Access).


  2. Use code similar to the following to open the recordset with the appendOnly option and add a new record to the table (this code can be used with the sample Northwind database):
    
         CMyRecordset rs;
         rs.Open(CRecordset::dynaset, NULL, CRecordset::appendOnly);
    
         CFile myBitmap("c:\\temp\\myBitmap.bmp", CFile::modeRead);
    		
         //Read from bitmap file into buffer.
         LONG nTotalBytes = myBitmap.GetLength();
    
         //Allocate a buffer for the input and read from file.
         BYTE * inputBuf = new BYTE[nTotalBytes];
         UINT nBytesRead = myBitmap.Read(inputBuf, nTotalBytes);
    	
         rs.AddNew();
    	
         rs.m_FirstName = "Cleo";
         rs.m_LastName = "Haw";
    
         //Copy the data into the CLongBinary member variable.
         LPVOID pVoid = NULL;
         DWORD bmpSize = nTotalBytes;
         rs.m_Photo.m_hData = ::GlobalAlloc(GMEM_MOVEABLE,bmpSize);
         pVoid = ::GlobalLock(rs.m_Photo.m_hData);
         ::memcpy(pVoid,inputBuf,bmpSize);
         ::GlobalUnlock(rs.m_Photo.m_hData);
         rs.m_Photo.m_dwDataLength =::GlobalSize(rs.m_Photo.m_hData);
    
         rs.SetFieldNull(&rs.m_Photo,FALSE);
         rs.SetFieldDirty(&rs.m_Photo,TRUE);
    
         rs.Update();
     
         delete [] inputBuf;
         ::GlobalFree(rs.m_Photo.m_hData); 


RESULTS: You will see that the record has been added but the binary field is Null.

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Sarah Parra, Microsoft Corporation


REFERENCES

Additional query words: blob updatable canappend


Keywords          : kbDatabase kbMFC kbODBC kbVC kbGrpVCDB 
Version           : winnt:5.0,6.0
Platform          : winnt 
Issue type        : kbprb 

Last Reviewed: August 5, 1999