Add photos to the database and add photos to the database
Author:Www.gudianxiaoshuo.com
This article is organized by the Code Assistant Software and released content is irrelevant to this software.More comfortable reading, more comfortable writing, and easier Publishing
The show-colored book and Text software is audible, readable, writeable, knowledgeable, and annotated. Coupled with the most powerful pure text color function in history, you can instantly show your book and text.
Add a photo to the database
PRecordSet. createInstance (_ uuidof (Recordset); strCmd. format (_ T ("SELECT * FROM class Table % d order by student ID"), m_pParentStudentInfo-> m_nClassID); pRecordSet-> Open (_ variant_t) strCmd, m_pConnection.GetInterfacePtr (), adOpenDynamic, adLockOptimistic, adshorttext); pRecordSet-> AddNew (); pRecordSet-> PutCollect (_ T ("student ID"), (long) nXuehao ); pRecordSet-> PutCollect (_ T ("name"), (maid) nameStr); pRecordSet-> PutCollect (_ T ("Address"), (maid) addStr ); pRecordSet-> PutCollect (_ T ("Contact Info"), (LPCTSTR) lianxiStr); pRecordSet-> PutCollect (_ T ("Remarks"), (LPCTSTR) beizhuStr ); CFile f; CFileException e; if (f. open (zhaoPianStr, CFile: modeRead | CFile: typeBinary, & e) // Open a jpg file {int nSize = f. getLength (); // first obtain the jpg file Length BYTE * pBuffer = new BYTE [nSize]; // apply for a memory on the heap based on the file size if (f. read (pBuffer, nSize)> 0) // Read the jpg file to pBuffer (applying for a block of memory on the stack) {BYTE * pBuf = pBuffer; /// the main section below is to put the jpg data in pBuffer into the database VARIANT varBLOB; SAFEARRAY * psa; SAFEARRAYBOUND rgsabound [1]; if (pBuf) {rgsabound [0]. lLbound = 0; rgsabound [0]. cElements = nSize; psa = SafeArrayCreate (VT_UI1, 1, rgsabound); for (long I = 0; I <(long) nSize; I ++) SafeArrayPutElement (psa, & I, pBuf ++); varBLOB. vt = VT_ARRAY | VT_UI1; varBLOB. parray = psa; pRecordSet-> GetFields ()-> GetItem (_ T ("photo")-> AppendChunk (varBLOB);} delete [] pBuffer; pBuf = NULL ;} f. close () ;}precordset-> Update ();
Read photo
CString xuehaoStr = m_XueShengList.GetItemText (nIndex, 0); // the text in the first column of the selected row _ CommandPtr spCmd; _ RecordsetPtr pRecordSet; spCmd. createInstance (_ uuidof (Command); spCmd-> ActiveConnection = m_pConnection; CString strCmd; strCmd. format (_ T ("select * from class Table % d where student ID = % d"), m_pParentStudentInfo-> m_nClassID, _ ttoi (xuehaoStr )); spCmd-> CommandText = (LPCTSTR) strCmd; pRecordSet = spCmd-> Execute (NULL, NULL, ad1_text ); If (pRecordSet-> adoEOF) // the student whose student ID is not found return; if (m_Pic.m_IPicture! = NULL) m_Pic.FreePictureData (); long nSize = pRecordSet-> GetFields ()-> GetItem (_ T ("photo")-> ActualSize; if (nSize> 0) {_ variant_t varBLOB; varBLOB = pRecordSet-> GetFields ()-> GetItem (_ T ("photo")-> GetChunk (nSize); if (varBLOB. vt = (VT_ARRAY | VT_UI1) {if (BYTE * pBuffer = new BYTE [nSize + 1]) {char * pBuf = NULL; SafeArrayAccessData (varBLOB. parray, (void **) & pBuf); memcpy (pBuffer, pBuf, nSize); SafeArrayUnaccessData (varBLOB. parray); m_Pic.LoadPictureData (pBuffer, nSize); delete [] pBuffer; pBuf = NULL; // The PICTURE control automatically adapts to the m_Photo.GetClientRect (rc); m_Photo.ClientToScreen (rc ); screenToClient (rc); long Width = 0; long Height = 0; m_Pic.m_IPicture-> get_Width (& Width); m_Pic.m_IPicture-> get_Height (& Height ); float f = Width/(float) Height; long w = f * (rc. bottom-rc.top); m_Photo.SetWindowPos (NULL, rc. left, rc. top, w, rc. height (), SWP_NOMOVE); m_Photo.GetClientRect (rc); CClientDC (& m_Photo); m_Pic.UpdateSizeOnDC (& dc); rc. left --; rc. top --; m_Pic.Show (& dc, rc );}}}