VC ++ ADO Development Practice

zhaozj2021-02-16  50

Access to BLOB data using ADO - ADO Development Practice

First, in the previous article "ADO First intimate contact" we detail the basic operation method of ADO, we often need to store larger binary data objects during actual development, such as: image, audio file Or other binary data, these data we call it binary large object Blob (Binary Large Object), and its access is different from ordinary data. This article will introduce the specific implementation process of accessing BLOB data in a database using ADO, and gives a complete example of image access display.

Second, the previous preparation first we create a table named userinfo, including three fields: ID, username, old, photo, where Photo is a field that can store binary data. 2.1 In SQL Server We can enter directly in Query Analyzer to create:

Create Table [DBO]. [UserPhoto] ([ID] [INT] Identity (1, 1) Not Null, [UserName] [varcha] (50) NULL, [OLD] [INT] null, [Photo] [Image] NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] where photo we are defined as a field of Image type.

2.2 The method created in Access is as follows: Create a new table includes ID, username, old, photo four fields, then open the table, select the design view in the table, set the ID to the automatic number, Username for text Old is a number, and Photo is OLE object. In our sample project already contains a library created ACCESS2000, you can use it directly.

Third, the specific step 3.1 Save the BLOB type of BLOB type cannot be stored in a normal way. We need to use the appendchunk function, and the appendchunk is included in the field object. The prototype is as follows: hResult appendchunk (const _variant_t & data); from the function prototype You can see that the key issue is that we need to assign binary data to variable variables, and we give specific code and make simple analysis:

// / Suppose the m_pbmpbuffer pointer points to binary data with a length of m_nfilelen, and has successfully opened the recordset object m_precordset /////

Char * pbuf = m_pbmpbuffer; variant varob; safectray * PSA; SafeArrayBound Rgsabound [1];

m_precordset-> addnew (); /// Add new record m_precordset-> Putcollect ("username", _ variant_t ("small plum"))); ////> Fill the username field m_precordset-> Putcollect ("OLD" for new records, _ variant_t ((long) 28); /// Fill the OLD field if (PBUF) {rgsabound [0] .llbound = 0; rgsabound [0] .CELEments = m_nfilelen; PSA = SafeArrayCreate (vt_ui1, 1, rgsabound); ///// Create a SafeArray object for (long i = 0; i <(long) m_nfilelen; i ) SafearrayPuteElement (PSA, & I, PBUF ); // Save the binary data pointed to the PBUF to the Safearray object PSA Varblob.vt = vt_Array | VT_UI1 ; /// The type of varblob is set to the array of Byte type varblob.parray = PSA; /// is assignd for Varblob variable m_precordset-> getfields () -> GetItem ("photo") -> appendchunk (varblob); // / Join the BLOB type Data} m_precordset-> update (); // / Save our data to the library to this library, our data has been successfully saved in the database, then what we have to do is to extract the data Let us continue! 3.2 BLOB data reads corresponding to the Appendchunk function we use when saving data, reading data should make With the getChunk function, getchunk's prototype is as follows: _variant_t getchunk (long length); Getchunk will return the Variant type variable containing the data after the length of the data, and then we can use the SafeAccessData function to get the CHAR * type of the data in the Variant variable. To facilitate our processing, the specific code is as follows:

转载请注明原文地址:https://www.9cbs.com/read-24308.html

New Post(0)