Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that 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.

↑ Back to the top


More information

The following file is available for download from the Microsoft Download Center:
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 additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591 How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

This sample uses the #import feature of Visual C++. 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 &amp;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(&amp;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();
				

↑ Back to the top


References

For additional information, please see the following article in the Microsoft Knowledge Base:
220152 Sample: ADOVC1 Simple ADO/VC++ Application

↑ Back to the top


Keywords: kbdownload, kbdatabase, kbfile, kbhowto, KB189415

↑ Back to the top

Article Info
Article ID : 189415
Revision : 8
Created on : 3/2/2005
Published on : 3/2/2005
Exists online : False
Views : 679