Using VC to realize the operation of super Long database fields

Source: Internet
Author: User
Tags function prototype ole
SummaryThis paper introduces the key technology of using VC to realize the operation method of super-long database field, and takes the database in Access2003 environment as an example, realizes under VC 6.0, and gives the key program code.

   Key WordsBinary large object; BLOB; Database Vc

   Preface

Database in the actual development process often need to store large binary data objects, such as images, audio files, video files or other binary data, which are called binary large object BLOBs (binary Large object), and its access to ordinary data is different. In a table in a database, blobs are actually stored as binary data. Because of the particularity of the Blob, the general program cannot handle it. For example, if you have a blob in a table, when you open it with Access or database DeskTop (Delphi in the Data management tool with you), the BLOB column will display only the Blob typeface. As for what data is actually stored in this column, access (database DeskTop) is not available. If you are using a control to open a table with a BLOB field in the program that we are compiling, the effect is the same. Our program is not able to display, edit, and insert BLOB fields directly. It can be seen that how to deal with the binary form data which can't be displayed directly is not satisfied with the conventional method.

Based on the Model library management system developed by the author, this paper introduces the database of Visual C + + 6.0 and Access 2003, and uses ADO to realize the access to the long database fields, including writing and reading.

   Design Database

Using Access2003 as the database system, the database is named Blob, and the only data table is a blob, as shown in Figure 1.

  
Figure 1

This includes four fields, namely ID (text), name (text), data (OLE object), and suffix (suffix), whose field type must be an OLE object. ID as the primary key, name is the file name of the Blob file, the data field is used to hold the binary large object, suffix is the suffix name of the binary file, can be Rm,avi,bmp,mp3, etc.

The implementation of the system:

1, SafeArray:

The SAFEARRAY structure is used when working with blobs. SAFEARRAY is a structured data type that contains an array of data elements that are made up of other data types. The security array is called because it contains the boundary information for each dimension and restricts access to the array elements within the bounds. Its Win32 definition safearray as follows:

typedef struct TAGSAFEARRAY
{
unsigned short cdims;
unsigned short ffeatures;
unsigned long cbelements;
unsigned long clocks;
void * PVDATA;
Safearraybound rgsabound[1];
} SAFEARRAY;

The members of this structure (cdims,clocks, etc.) are set up and managed through API functions. The real data is stored in the Pvdata member, and the SAFEARRAYBOUND structure defines the details of the array structure. The following is a brief description of the struct member:

Members Describe
Cdims Number of dimensions of an array
Ffeatures Flags used to describe how arrays are allocated and how they are released
Cbelements Size of array elements
Clocks A counter that tracks how many times the array has been locked
PvData Pointer to data buffer
Rgsabound Describes the array structure of each dimension of the array, whose size is variable

Rgsabound is an interesting member, its structure is not very intuitive. It is an array of data ranges. The size of the array differs according to the safe array dimension. The Rgsabound member is an array of safearraybound structures-each element represents a dimension of SAFEARRAY.

typedef struct TAGSAFEARRAYBOUND
{
unsigned long celements;
unsigned long llbound;
} safearraybound;


The dimension is defined in the Cdims member. For example, the dimension of an array of ' C ' classes can be [3][4][5]-a three-dimensional array. If we use a SAFEARRAY to represent this structure, we define a rgsabound array with three elements-one for one dimension. Cdims = 3; Safearraybound rgsabound[3]; Rgsabound[0] element defines the first dimension. In this example, the Ilbound element is 0, which is the lower bound of the array. The value of the Celements member is equal to three. The second dimension of the array ([4]) can be defined by the second element of the rgsabound structure. The nether can also be 0, the number of elements is 4, the third dimension is the same.

2. Write the binary file to the database:

Because this binary file can be large, it is not possible to read all the content into memory at once. We need to read multiple times, each time we can read a packet from a file using the function Cfile::read (), and then call the APPANDCHRNK () function of the Field object to read the package into the database. The APPANDCHRNK () function is included in the Field object and is prototyped as follows: HRESULT AppendChunk (const _variant_t & Data); The key problem that we can see from the function prototype is that we need to assign the binary data to the variant type. The key code to implement is as follows:

while (1)
{uisread=f.read (bval,chunksize);
if (uisread==0) break;
Rgsabound[0].celements =uisread; rgsabound[0].llbound = 0;
PSA = safearraycreate (vt_ui1,1,rgsabound);///Create SAFEARRAY Object
for (long index=0;index Saf Earrayputelement (Psa,&index,&bval[index]);
Varchunk.vt = vt_array| VT_UI1;
Varchunk.parray = PSA;   
//Join BLOB type data
M_precordset->fields->getitem ("Data")->appendchunk (varchunk);
:: VariantClear (&varchunk);
:: Safearraydestroydata (PSA);
if (uisread< }


All of our read-in data work is implemented in a while loop, and each time a packet is read into the data, the amount of data is chunksize the length of the binary data. Where *pbuf is the pointer to the buffer, which is the packet to be read into. The chunksize is a variant variable that holds the binary data, and the PSA is a pointer to the safe array safearray.

3. Read the binary object from the database to the file:

Similarly, since this binary object may be large, it is not possible to read all the contents one at a time in memory corresponding to the AppendChunk function we used when saving the data, the read data should use the GetChunk function, the GetChunk prototype is as follows:

_variant_t GetChunk (long Length); the only parameter Length represents the number of bytes that need to be read.

The key code to implement is as follows:

Long lblobsize = m_precordset->fields-> item["Data"]->actualsize;
while (lblobsize>0)
{
lisread= lblobsize >=chunksize? Chunksize:lblobsize;
Get a packet from the field data
Varchunk = m_precordset->fields->item["Data"]->getchunk (Lisread);
for (index=0;index :: Safearraygetelement (Varchunk.parray,&index,buf+index);
Writing a packet to a file
F.write (Buf,lisread);
Lblobsize-=lisread;
}


Where F is a CFile object that represents the file to be stored. The long variable lblobsize records the size of the binary object. Use a while loop to read the Lisread bytes from the database every time, until they are all read out.

  Conclusion

Because of the rapid development of information technology, especially the widespread use of multimedia technology, it can be foreseen that the application of large objects in the database will become more and more common, and the access of large objects will inevitably be a developing direction of database technology. This paper discusses the VC implementation of binary large object blob storing and reading in the database, which can meet the basic system requirements, has a certain portability, and provides a reference basis for more extensive application and further research in the future.

Transferred from: http://dev.yesky.com/471/2590971_2.shtml

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.