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