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.
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值