About the use of VC, VB to store data (binary large object) storage database

xiaoxiao2021-03-06  52

:

Someone often asks how to deposit images into the database, I will not be essential. After multi-directional guidance and its own exploration during the development process, it is finally solved. Here is the original code for how to use VC, VB to operate the image file to access the database, help some friends who have not mastered the method, and give me a lot of experts in this way.

(Both ADO Connection Database) 1. VC stores a file into the database cfile refilefile; if (0 == ImageFile.Open ("D: //user//bmp.bmp", cfile :: moderad) Return; _RecordSetPtr pRs = NULL; _ConnectionPtr pConnection = NULL; _variant_t varChunk; HRESULT hr; BYTE * pbuf; long nLength = imagefile.GetLength (); pbuf = new BYTE [nLength 2]; if (pbuf == NULL) return; // allocate memory error; imagefile.Read (pbuf, nLength); // read the file into memory BYTE * pBufEx; pBufEx = pbuf; // build a SAFFERRAY SAFEARRAY * psa; SAFEARRAYBOUND rgsabound [1]; rgsabound [0] .lLbound = 0 ; rgsabound [0] .cElements = nLength; psa = SafeArrayCreate (VT_UI1, 1, rgsabound); for (long i = 0; i Open (Strcn, ",", NULL); // connect A Database PRS.CREATEINSTANCE (__ uuidof (recordset)); PRS-> Open (" CustomInfo, _ variant_t (idispatch *) PConnection, true, adopenkeyset, adlickoptimistic, adcmdtable; // open a Table // PRS-> addNew (); prs-> fields-> getItem ("image") -> appendchunk (Varblob); PRS-> Update (); PRS-> Close (); pConnection-> Close ();} catch (_COM_ERROR &

e) {// notify the user of errors if any. _bstr_t bstrsource (e.Source ()); _BSTR_T BSTRDESBRIPTION (E.DESCRIPTION ()); CString SERROR; SERROR.FORMAT ("Source:% s / n Description:% s / n ", (LPCSTR) bstrSource, (LPCSTR) bstrDescription); AfxMessageBox (sError);} 2. VC IMAGE field in the database stored in a file taken _RecordsetPtr pRs = NULL; _ConnectionPtr pConnection = NULL; _variant_t varChunk; HRESULT hr; VARIANT varBLOB; _bstr_t strCnn ( "Provider = SQLOLEDB.1; Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = CUSTOM; Data Source = SERVER"); try {// Open a connection pConnection.CreateInstance (__ uuidof (Connection ))); Hr = pConnection-> Open (strunn, ",", null); PRS.CREATEINSTANCE (__ uuidof (recordset)); PRS-> Open ("CustomInfo", _ variant_t ((idispatch *) PConnection, True) , AdopenKeyset, Adopoptimistic, Adcmdtable; // Read Data Long LDATALENGTH = PRS-> Fields-> GetItem ("Image") -> Actualsize; Varblob = PRS-> getfields () -> GetItem ("Image") -> getchunk (ldatalength); if (varblob.vt == (vt_array | vt_ui1)) {byte * Pbuf = null; PBUF = (Byte *) GlobalLococococcoc (GMEM_FIXED, lDataLength); SafeArrayAccessData (varBLOB.parray, (void **) pBuf); // Build a File in Windows Temp Directory char tmpPath [_MAX_PATH 1]; GetTempPath (_MAX_PATH, tmpPath); CString strFileName = "temp. BMP "; strfilename = tmp path strfilename;

CFile outFile (strFileName, CFile :: modeCreate | CFile :: modeWrite); LPSTR buffer = (LPSTR) GlobalLock ((HGLOBAL) pBuf); outFile.WriteHuge (buffer, lDataLength); GlobalUnlock ((HGLOBAL) pBuf); outFile.Close (); SafearrayunaccessData (varblob.parray);} PRS-> Close (); PConnection-> Close ();} catch (_COM_ERROR & E) {// notify the user of errors if any. _Bstr_t bstrsource (E.Source () ); _Description (); cstring serror; serror.format ("Source:% S / N Description:% S / N", (LPCSTR) BSTRSOURCE, (LPCSTR) BSTRDESBRIPTION; AFXMESSAGEBOX (SERROR); } 3. VB IMAGE the documents stored in the database fields Sub savepic (FileName As String, IndexNumber As Long) Dim DcnNWind As New ADODB.Connection Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset DcnNWind.CursorLocation = adUseClient DcnNWind.Open "Provider = SQLOLEDB.1; Integrated Security = SSPI; PERSIST Security Info = False; Initia l Catalog = CUSTOM; Data Source = SERVER "rs.CursorType = adOpenKeyset rs.LockType = adLockOptimistic rs.Open" CustomInfo ", DcnNWind,, adCmdTable rs.Move (IndexNumber) Call FileToBlob (rs.Fields (" Image "), FileName , FileLen (FileName)) rs.UpdateBatch adAffectCurrentEnd Sub Private Sub FileToBlob (fld As ADODB.Field, FileName As String, Optional ChunkSize As Long) Dim fnum As Integer, bytesLeft As Long, bytes As Long Dim tmp () As Byte If ( FLD.Attributes and adfldlong) = 0 Then Err.raise 1001, "Field Doesn't Support The getchunk method." end if fnum =

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

New Post(0)