Using ADO to realize the access of BLOB data-ado development Practice Two

Source: Internet
Author: User
Tags function prototype

First, the preface

In the last article "ADO First intimate contact" we introduced in detail ADO basic operation method, in the actual development process we often need to store large binary data objects, such as: images, audio files, or other binary data, which we call binary large object blob ( Binary Large Object, the way it is accessed differs from ordinary data. This article will introduce the realization process of using ADO to access BLOB data in the database, and give the complete example project of realizing the image access display.

Ii. Preliminary Preparation

First we create a table named UserInfo, which contains three fields: Id,username,old,photo, where photo is a field that can store binary data.

2.1 In SQL Server, we can directly enter the following statement in Query Analyzer to create:

CREATE TABLE [dbo].[userphoto] (
  [id] [int] IDENTITY (1, 1) NOT NULL ,
  [username] [varchar] (50) NULL ,
  [old] [int] NULL ,
  [photo] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Where photo we define as the image type of the field.

2.2 The methods created in Access are as follows:

Create a new table including Id,username,old,photo four fields, then open the table, select View menu Design view, set the ID to automatically numbered incremented long integer, username is text, old is numeric, photo is OLE object.

In our sample project we already have a library of ACCESS2000 that you can use directly.

Iii. Specific steps

Preservation of 3.1 BLOB data

Blob-type data cannot be stored in a normal way, and we need to use the AppendChunk function, AppendChunk included in the Field object, and the prototype is as follows:

HRESULT AppendChunk (const _variant_t & Data);

The key problem from the function prototype is that we need to assign the binary data to variant variables, and we'll give you a simple analysis of the code below:

Suppose the M_pbmpbuffer pointer points to binary data with a length of M_nfilelen, and the Recordset object has been successfully opened m_precordset///
char *pbuf = M_pbmpbuffer;
VARIANT Varblob;
SAFEARRAY *PSA;
Safearraybound rgsabound[1];
M_precordset->addnew (); Add new record
M_precordset->putcollect ("username", _variant_t ("Xiao Li")); Populate username fields for new records
M_precordset->putcollect ("old", _variant_t ((long) 28); Fill Old field
if (PBUF)
{
Rgsabound[0].llbound = 0;
Rgsabound[0].celements = M_nfilelen;
PSA = SafeArrayCreate (VT_UI1, 1, rgsabound); Creating SAFEARRAY objects
for (long i = 0; I < (long) M_nfilelen; i++)
SafeArrayPutElement (PSA, &i, pbuf++); To save Pbuf Point binary data to the SAFEARRAY object PSA
VARBLOB.VT = Vt_array |                  VT_UI1; Set the type of Varblob to an array of type Byte
Varblob.parray = PSA; Assigning values to Varblob variables
M_precordset->getfields ()->getitem ("Photo")->appendchunk (Varblob);///Add data of BLOB type
}
M_precordset->update (); Save our data to the library

Now that our data has been successfully saved to the database, the next thing we need to do is extract the data and let's go ahead!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.