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

zhaozj2021-02-11  149

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. (All use ADO connection database)

1. VC deposits a file into the database

CFILE ImageFile; 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); // be called 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

_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 (strcnn, ",", 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", (LPCST) BSTRSource, (LPCST) BSTRDESBRIPTION; AFXMESSAGEBOX (SERROR);} 2. VC Put the image in the database Save as file

_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 (strcnn, ",", null); prs.createInstance (__ uuidof (recordset); PRS-> Open ( "customInfo", _ variant_t ((IDispatch *) pConnection, true), adOpenKeyset, adLockOptimistic, 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 *) GlobalAlloc (GMEM_FIXED, LDATALENGTH; SafeAccessData (Varblob.Parray, (void **) PBUF); // build a file in Windows TE mp Directory char tmpPath [_MAX_PATH 1]; GetTempPath (_MAX_PATH, tmpPath); CString strFileName = "temp.bmp"; strFileName = tmpPath strFileName; CFile outFile (strFileName, CFile :: modeCreate | CFile :: modeWrite); LPSTR buffer = (LPSTR) Globallock ((Hglobal); Outfile.Writehuge (Buffer, LDATAGTH); 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 ()); _BSTR_T BSTRDESBRIPTION (E.DESCRIPTION ()) ; Cstring serror; serror.format ("Source:% S / N Description:% S / N", (LPCST) BSTRSOURE, (LPCSTR) BSTRDESBRIPTION; AFXMESSAGEBOX (SERR);} 3. VB stores files into the database image field

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; Initial 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 does not support the GetChunk method." End If fnum = FreeFile Open FileName For Binary As fnum bytesLeft = LOF (fnum) Do While bytesLeft bytes = bytesLeft If bytes> ChunkSize Then bytes = ChunkSize ReDim tmp (1 TO BYTES) AS BYTE GET # 1, TMP FLD.APPENDCHUNK TMP BYTESLEFT = BYtesLes - Bytes Loop Close #FNUMEND SUB

4. VB writes the file from the Image field.

Sub loadpic (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 = SSI; Persist Security Info = False; Initial Catalog = CUSTOM; Data Source = SERVER "rs.CursorType = adOpenKeyset rs.LockType = adLockOptimistic rs.Open" customInfo ", DcnNWind,, adCmdTable rs.Move (IndexNumber) Call BlobToFile (rs.Fields (" Image " ), "c: /windows/temp/tmp.bmp", rs.fields ("image"). Actualsize) End Subprivate Sub Blobtofile (FLD AS AdoEld, 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 IF DIR $ (FileName) <> "" "THEN KILL FILENAME FOR BINARY As Fnum Bytesleft = fld.actualsize do while bytesleft bytes = bytesleft IF bytes> chunksize dam = chunksize tmp = fld.getchunk (bytes) Put #fnum, tmp bottesleft = bytesLoP close #fnumend Sub

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

New Post(0)