How to put images in the database !!
---------------------------------------------------------------
First, create a table named userinfo, which contains three fields: ID, username, old, and photo. photo is a field that can store binary data.
2.1 In SQL Server, you can directly enter the following statement in query analyzer to create the statement:
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]
Here, photo is defined as an image field.
2.2 The method used to create an access interface is as follows:
Create a new table that includes four fields: ID, username, old, and photo. Open the table, select design view from the View menu, and Set ID to auto-numbered incremental long integer and username as text, old is a number and photo is an OLE object.
The example project contains a database named Access2000, which can be used directly.
Procedure
1. Save BLOB Data
BLOB data cannot be stored in normal mode. We need to use the AppendChunk function. The AppendChunk is included in the Field object. The prototype is as follows:
Hresult AppendChunk (const _ variant_t & data );
From the function prototype, we can see that the key problem is that we need to assign binary data to variant type variables. Below we will provide specific code for simple analysis:
/// Assume that the m_pbmpbuffer Pointer Points to a binary data with a length of m_nfilelen, and the record set object m_precordset has been successfully opened ///
Char * pbuf = m_pbmpbuffer;
Variant varblob;
Safearray * PSA;
Safearraybound rgsabound [1];
M_precordset-> addnew (); // Add a new record
M_precordset-> putcollect ("username", _ variant_t ("Xiao Li"); // fill in the username field for the new record
M_precordset-> putcollect ("old", _ variant_t (long) 28); // fill in 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 ++)
Safearrayputelement (PSA, & I, pbuf ++); // Save the binary data that pbuf points to the safearray object PSA
Varblob. Vt = vt_array | vt_ui1; // sets the varblob type to an array of the byte type.
Varblob. parray = psa; // assign a value to the varblob variable
M_precordset-> getfields ()-> getitem ("photo")-> AppendChunk (varblob); // Add BLOB Data
}
M_precordset-> Update (); // save our data to the database
Reading BLOB Data
Corresponding to the AppendChunk function used to save data, the getchunk function should be used to read data. The prototype of getchunk is as follows:
_ Variant_t getchunk (long length );
After the length of the data is given, getchunk will return the variant type variable containing the data. Then, we can use the safearrayaccessdata function to obtain the char * type pointer pointing to the data in the variant variable to facilitate our processing, the Code is as follows:
Long ldatasize = m_precordset-> getfields ()-> getitem ("photo")-> actualsize; // get the Data Length
If (ldatasize> 0)
{
_ Variant_t varblob;
Varblob = m_precordset-> getfields ()-> getitem ("photo")-> getchunk (ldatasize );
If (varblob. Vt = (vt_array | vt_ui1) // you can check whether the data type is correct.
{
Char * pbuf = NULL;
Safearrayaccessdata (varblob. parray, (void **) & pbuf); // get the pointer to the data
/***** Here we can process the data in pbuf *****/
Safearrayunaccessdata (varblob. parray );
}
}