FILE: AdoChunk.exe Using GetChunk and AppendChunk in Visual C++

ID: Q189415

The information in this article applies to:

SUMMARY

AdoChunk.exe is a sample that demonstrates using the GetChunk and AppendChunk methods with Visual C++ version 5.0. The sample also demonstrates the use of recordset navigation functions.

MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

 ~ Adochunk.exe (size: 128512 bytes) 

NOTE: After you click the preceding link, please select "Save this program to disk" from the File Download dialog box. Next, click Run on the Start menu and run the .exe file with the -d attribute, as shown in the following example:

   C:\<directory name>\Adochunk.exe

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services

This sample uses the #import feature of Visual C++ version 5.0. This is a MFC dialog-based application. It operates on a Microsoft Access database table having two columns; "Name" of type Text, and "BlobData" of type OLE Object.

The following files are included in the sample project:

   File Name               Description
   ------------------------------------------------------------------------

   CAdoVcChunkDlg.h        CDialog derived class declaration. This class
                           has a member variable of type _RecordsetPtr

   CAdoVcChunkDlg.cpp      Implementation for the Dialog with different
                           message handlers using ADO and updating the
                           different controls.

   CAdoVcChunk.h           CWinApp derived class header file.

   CAdoVcChunk.cpp         CWinApp derived class.


Dialog has several buttons for different actions like navigating within the recordset and closing the application. An explanation of the click actions on the different buttons follows:

   MoveNext, MovePrevious, MoveFirst and MoveLast:

      Clicking these buttons enables the user to navigate through the
      recordset. Navigating from one record to another saves the record
      data if the record is modified.

   Browse:

      Displays a File Open dialog box. Selecting a file to open fills the
      contents of the edit box (showing the details column data) with the
      contents of the file selected.

   AddNew:

      Adds a new record with data present in the different
      controls of the dialog.

   Close:

     Closes the recordset.


Here are four of the utility functions included in this sample:

   DumpError(_com_error &e):

      This function is called from all the other functions in the
      _com_error exception handler. This function retrieves information on
      the error and displays an error message box.

   Update():

      This function retrieves data from the recordset and updates the
      different controls. The function uses the GetChunk method to retrieve
      the BlobData field. The data retrieved from the BlobData field is a
      Safearray of bytes. The code for this function follows:


         // Get the value of the first field.
         varName = m_pRs->Fields->Item[0L]->Value;
         // Initialize the data member variable.
         m_csName = varName.bstrVal;

         // Get the actual length of the data.
         lDataLength = m_pRs->Fields->Item[1L]->ActualSize;
         if(lDataLength)
         {
            VariantInit(&varBLOB);
            // Get the chunk of data in the second field.
            varBLOB = m_pRs->Fields->Item[1L]->GetChunk(lDataLength);

            // If the data retrieved is array of bytes then get data from
            // the array and set the data value variable of the edit box.
            if(varBLOB.vt == (VT_ARRAY | VT_UI1))
            {
              // Lock the safe array data.
              SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
              // Initialize the data member variable with the data.
              m_csValue = pBuf;
              // Unlock the safe array data.
              SafeArrayUnaccessData(varBLOB.parray);
            }
         }
            // Update the controls with the data in the member variables.
         UpdateData(FALSE);


   BlobToVariant(VARIANT &varArray):

      This function reads the binary data from the edit control and
      converts it to a variant containing a SafeArray of bytes.

   Save():

      This function reads data from the edit controls and updates the
      database. The function is called whenever navigation action is
      performed. This function uses the AppendChunk method to update data
      to the database. The code is as follows:

         // Call this function to get the blob data in the edit box as
         // variant containing SafeArray.
         BlobToVariant(varBLOB);
         // Set the value of the field.
         m_pRs->Fields->Item[1L]->AppendChunk(varBLOB);
         m_pRs->Update();

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q174565
   TITLE     : FILE: Adovc.exe Demonstrates How to Use ADO with Visual C++

Additional query words: kbADO150 kbDatabase kbVC500
Version           : WINDOWS:1.5; WINDOWS: 5.0
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: July 20, 1998