Insert raw data into a binary data field with ADO

With ADO it’s easy to append some common data type in to a field, such as char, int, short, string, double and so on. Just use the following sentences:

 

 

But when it comes to the binary raw data, or large text, you need to do a little more job. One way to accomplish this is using SAFEARRAY provided by COM.

 

1.1.    Some introduction of SAFEARRAY

The data structure of SAFEARRAY:

 

 

The array rgsabound is stored with the left-most dimension in rgsabound[0] and the right-most dimension in rgsabound[cDims - 1]. If an array was specified in a C-like syntax as a [2][5], it would have two elements in the rgsabound vector. Element 0 has an lLbound of 0 and a cElements of 2. Element 1 has an lLbound of 0 and a cElements of 5.

The fFeatures flags describe attributes of an array that can affect how the array is released. The fFeatures field describes what type of data is stored in the SAFEARRAY and how the array is allocated. This allows freeing the array without referencing its containing variant. The bits are accessed using the following constants:

fFeatures Flags

Description

FADF_AUTO 0x0001

An array that is allocated on the stack.

FADF_STATIC 0x0002

An array that is statically allocated.

FADF_EMBEDDED 0x0004

An array that is embedded in a structure.

FADF_FIXEDSIZE 0x0010

An array that may not be resized or reallocated.

FADF_RECORD 0x0020

An array that contains records. When set, there will be a pointer to the IRecordinfo interface at negative offset 4 in the array descriptor.

 

The SAFEARRAYBOUND Structure Represents the bounds of one dimension of the array. The lower bound of the dimension is represented by lLbound, and cElements represents the number of elements in the dimension. The structure is defined as follows:

 

 

lLbound indicates the beginning of the array.

From http://msdn.microsoft.com/en-us/library/ms221482.aspx

 

1.1.    How to insert:

Step1: create a SafeArray.

We call SafeArrayCreate to create a VT_VARIANT[5] array as follow:

 

 

 

if we only need a one-dimensional array, just call SafeArrayCreateVector as follow:

 

 

step 2: fill the array with data
Step 3: put the array into record field
 
Step 4: release the array
Caution we need to set the varChunk to NULL first. Or else error will happen when destruction of varChunk is called.
 
發佈了77 篇原創文章 · 獲贊 3 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章