I have been studying Delphi recently and I don't know much about it. In the past few days, data in Oracle needs to be stored and read in binary mode. I found a lot of posts to find what I needed, but they were not all at all. I was not very familiar with it for several days. Sort it out for reference by yourself and everyone in the future.
First, olevariant is converted to stream and saved to the database. The control function I call gets an olevariant type, and the storage can only save the stream, so it can only be converted.
Procedure tform1.varianttostream ();
VaR
O: olevariant;
P: pointer;
S: stream;
Begin
Mycontrol. savetostream (o); // This is a function of the control I use. It is used to write the binary data I need into the o variable. The type is olevariant;
If O = NULL then // I do not know if this is true. I also say isnull and empry functions on the Internet, but I have reported errors when using them.
Begin
Application. MessageBox ('no data is available. Check it! ',' Prompt! ');
End;
Savequery. SQL. Clear;
Savequery. SQL. Text: = 'select ID, name, content from mytab where 0 = 1 ';
Savequery. open;
Savequery. append;
Savequery. fieldbyname ('id'). asstring: = '01 ';
Savequery. fieldbyname ('name'). asstring: = 'Demo ';
P: = vararraylock (O );
Try
S: = tmemorystream. Create;
S. Position: = 0;
S. writebuffer (P ^, vararrayhighbound (O, 1) + 1 );
Tblobfield (savequery. fieldbyname ('content'). loadfromstream (s );
Finally
Vararrayunlock (O );
End;
Savequery. post;
End;
Below is the conversion from stream to Variant
Procedure tfrom1.streamtovariant ();
VaR
S: tmemorystream;
V: variant;
P: pointer;
Begin
S: = tmemorystream. Create;
Savequery. SQL. Clear;
Savequery. SQL. Text: = 'select content from mytab where I = ''01 ''';
Savequery. open;
If savequery. recordcount> 0 then
Begin
(Savequery. fieldbyname ('content') as tblobfield). savetostream (s );
V: = vararraycreate ([0, S. Size-1], varbyte );
P: = vararraylock (v );
S. Position: = 0;
S. Read (P ^, S. size );
Vararrayunlock (v );
Mycontrol. openstream (v, 2); // This function is the function used by the control to read stream. It is just a demonstration that can be used directly.
End;
End;
Welcome to their mutual powder, Sina Weibo: http://weibo.com/3313648752