Operations on olevariant, variant, and stream in Delphi

Source: Internet
Author: User

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

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.