C ++ writes the Blob field of the Oracle database and reads the blob to a local file.

Source: Internet
Author: User
Tags 04x

Write local files to the database

Void dlgimproadsection: onokbtnclicked () {# pragma region verification cstring tempstr; cstring filename; m_filepath.getwindowtext (filename); If (access (const char *) _ bstr_t (filename ), 0 )! = 0) {ads_alert (L "file does not exist! "); Return;}... # pragma endregion # pragma region reads files in binary format. Store varblobcfile file; If (file. open (filename, cfile: moderead | cfile: typebinary) = false) {return;} Long filelen = file. getlength (); // file length // pointer for storing the binary stream of the file byte * filebuffer = new byte [filelen + 1]; file. read (filebuffer, filelen); file. close (); // specify the boundary safearraybound rgsabound of a one-dimensional security array (Safe array) [1]; rgsabound [0]. llbound = 0; rgsabound [0]. celements = filelen; // security array safearray * safearray; safearray = safearra Ycreate (vt_ui1, 1, rgsabound); For (long I = 0; I <filelen; I ++) {safearrayputelement (safearray, & I, filebuffer ++ );} // data of the Blob type stored in the database. Set to byte array variant varblob; varblob. vt = vt_array | vt_ui1; varblob. parray = safearray; # pragma endregion # pragma Region insert record cstring SQL = l "select * From table_name where 1 = 0"; _ recordsetptr RS; RS. createinstance (_ uuidof (recordset); try {If (RS-> open (SQL. getbuffer (0), g_conn.getinterfaceptr (), adopendynamic, adlockoptimistic, adshorttext) = s_false) {return;} guid; cocreateguid (& guid); cstring ID; id. format (L "% 08x % 04x % 04x % 02x % 02x % 02x % 02x % 02x % 02x % 02x % 02x", guid. data1, guid. data2, guid. data3, guid. data4 [0], guid. data4 [1], guid. data4 [2], guid. data4 [3], guid. data4 [4], guid. data4 [5], guid. data4 [6], guid. data4 [7]); RS-> addnew (); RS-> putcollect ("ID", _ variant_t (ID); RS-> putcollect ("... ",...); RS-> putcollect ("cad_file", _ variant_t (""); RS-> getfields ()-> getitem ("file")-> AppendChunk (varblob ); if (RS-> Update () = s_ OK ){...} else {...}} catch (...) {...} rs = NULL; # pragma endregioncdialog: onok ();}

Read the Blob field value of the database and save it to a local file.

# Pragma region reads files from the database and saves them to the local cstring tempfile; // the file name to be stored in the local file. _ Recordsetptr RS; RS. createinstance (_ uuidof (recordset); cstring SQL; SQL. format (L "select file from tablename where id = '% S'", ID); try {If (RS-> open (SQL. getbuffer (0), g_conn.getinterfaceptr (), adopendynamic, adlockoptimistic, adshorttext) ==s_false) {return false;} If (RS-> adoeof) {return false ;} long datasize = RS-> getfields ()-> getitem ("file")-> actualsize; If (datasize <0) {return false ;} _ variant_t varblob = RS-> getfields ()-> getitem ("file")-> getchunk (datasize); If (varblob. vt = (vt_array | vt_ui1) // determines whether the data type is correct {char * pbuf = NULL; safearrayaccessdata (varblob. parray, (void **) & pbuf); // obtain the cfile cadfile pointing to the data; tempfile = apppath + "\ temp. DWG "; If (cadfile. open (tempfile, cfile: modecreate | cfile: modewrite | cfile: typebinary) = false) {return false;} cadfile. write (pbuf, datasize); cadfile. close (); safearrayunaccessdata (varblob. parray) ;}} catch (...) {return false;} # pragma endregion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.