Microsoft KB Archive/282932

= AdoSBin.exe Stores and Retrieves Short Binary Data with ADO in Visual C++ =

Article ID: 282932

Article Last Modified on 8/10/2004

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7

-



This article was previously published under Q282932



SUMMARY
The AdoSBin.exe sample demonstrates how to retrieve and store short binary data with ActiveX Data Objects (ADO). When you use ADO, binary data is usually retrieved and stored by using the GetChunk and AppendChunk methods of the Field object. However, if the binary data is small, it is not necessary to retrieve the data in chunks. The data can be retrieved directly through a safe array.

This approach is usually used if the data in the data store is not really binary in nature but instead represents large integers or custom datatypes such as the SQL Server timestamp datatype. When you use custom datatypes, ADO retrieves them as a safe array of unsigned bytes, which can easily be converted to either large integer types or byte arrays.



MORE INFORMATION
The AdoSBin.exe Visual C++ sample demonstrates how to retrieve and store short binary data from a Microsoft SQL Server 7.0 database. The SQL Server datatypes that are used are:
 * timestamp
 * binary(8)
 * binary(16)

The timestamp and binary(8) datatypes are retrieved into a Visual C++ large integer type (ULONGLONG) and the binary(16) datatype is retrieved into a byte array.

To run the sample, use these steps:  Extract the project files. Make the necessary changes to the connection string to reflect your SQL Server database (the one you want to use for this sample, and the user/password information). By default, the program attempts to connect to a SQL Server server on the local computer and use the pubs database. The program then attempts to connect with the sa login account, and then creates a table named ShortBinaryTable in the specified database.  If you do not want to drop the table that is created prior to the termination of the program, comment out the line that drops the table towards the end of the main function: pCon->Execute(btDrop, NULL, -1);  Compile the program. If you keep the statement to drop the table, place a breakpoint before that line so that you can observe the new data in the SQL Server Query Analyzer before the table is dropped. Run the program.

The program creates the table, inserts a single record with some binary data by using an INSERT statement, retrieves the data with ADO, and then inserts another record with ADO. Lastly, the program drops the table.</ol>

Programming Notes
<ul>  All three fields that are to be retrieved from the table are fetched by ADO as binary data. The code checks whether the data is indeed binary by checking the type of the VARIANT. Binary data is represented in a VARIANT as a safearray of bytes, hence VT_ARRAY | VT_UI1: if (val.vt == (VT_ARRAY | VT_UI1)) //only process binary data ...                       </li>  Both the timestamp field and the binary(8) field can be interpreted as 64-bit integers. Visual C++ provides two datatypes for 64-bit integers, LONGLONG (__int64) and ULONGLONG (unsigned __int64). This sample interprets the data as ULONGLONG.

When the binary data is to be converted to an integer, the order of the bytes have to be reversed because the bytes in the array are ordered with the most significant byte first. Personal computers store numbers in the big-endian format with the most significant byte last, so a function is provided to convert the data from an array of bytes to an integer called VarToULongLong. The VarToULongLong function uses the SafeArrayAccessData and SafeArrayUnaccessData functions to obtain a pointer to the data contained in the array and then loops through the bytes in the array to fill the bytes of the ULONGLONG integer.

for (int i=0; i < 8; i++) {       ((BYTE*) (&ullVal)) [i] = ((BYTE*) (pvData)) [7-i]; }                       </li>  The binary(16) field is interpreted as a byte array. The VarToByteArray function again uses the SafeArrayAccessData and SafeArrayUnaccessData functions to obtain a pointer to the data contained in the array but does not reverse the order of the bytes: for (int i=0; i < v.parray->rgsabound[0].cElements; i++) {       pBytes[i] = ((BYTE*) (pvData)) [i]; }                       </li> When inserting new data, the ULongLongToVar and ByteArrayToVar functions are used to create the variants that contain the safearray. A helper function named CreateSafeArrayU1 is provided to simplify the creation of the C++ SAFEARRAY object.</li> The PrintULongLong and PrintByteArray functions use the cout stream to output the data in hexadecimal format.</li></ul>

The following file is available for download from the Microsoft Download Center:

AdoSBin.exe

Release Date: FEB-01-2001

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. The AdoSBin.exe file contains the following files:

Use the adVarBinary datatype with the ADO Command object when you pass the short binary data types as parameters to a stored procedure. For additional information%1, click the article number%2 below to view the article%2 in the Microsoft Knowledge Base:

%3 %4

Additional query words: AdoSBin EXE

Keywords: kbhowto kbdownload kbdatabase kbfile kbsample KB282932

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.