Old Problem: saving images by SQL. Save and read it again. It is found that the stream size is inconsistent. Although old, no results are found.

Source: Internet
Author: User
Old Problem: saving images by SQL. Save and read it again. It is found that the stream size is inconsistent. Although old, no results are found. Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiDB/html/delphi_20061217121231255.html
Tclientdataset
Save as follows
VaR
Tempclient: tclientdataset;
Tempjpeg: tsf-image;
Tempstream: tmemorystream;
....
Begin
....
Tempjpeg. loadfromfile ('d: \ 1.jpg ');
Tempjpeg. savetostream (tempstream );
Tempclient. commandtext: = 'insert into mytest (imageid, imagesize, image) values (: ID,: Size,: imagedata )';
Tempclient. Params. parambyname ('id'). asinteger: = tempid;
Tempclient. Params. parambyname ('SIZE'). asinteger: = tempstream. size;
Tempclient. Params. parambyname ('imagedata'). loadfromstream (tempstream, ftgraphic );
// The size of the corresponding streaming is the same as that of 1.jpg. The stream size is 447244
Tempclient. Execute;
....
End;

Read
VaR
Tempclient: tclientdataset;
Begin
Tempclient: = tclientdataset. Create (NiL );
Try
Tempclient. commandtext: = 'select imagesize, image from mytest where id = 1 ';
Tempclient. open;
If not tempclient. fieldbyname ('image'). isnull then
Begin
Tblobfield (tempclient. fieldbyname ('image'). savetostream (tempstream );
// The size of the tempsteam file I read is different from that of the original JPEG file. Save the stream to a file and find that the data is not correct at all. The size of the original file is 447244, and the size of the stream obtained from the database is 550262.
End;
Fianlly
...
End;
End;

The database is SQL 2000 and uses ADO connection.

Try to save and read them directly.
Tempclient. Params. parambyname ('imagedata'). loadfromfile ('d: \ 1.jpg ');

Tblobfield (tempclient. fieldbyname ('image'). savetofile ('d: \ 1.jpg ');

Thank you.

The result of returning xxmmmx (kicking) () Direct loadfromefile is the same as that of savetofile.
The new files saved by the savetofile are still larger. Data also changed

I tested it. No problem. However, I use adoquery;

VaR
Tempjpeg: tsf-image;
Tempstream: tmemorystream;
Begin
Tempstream: = tmemorystream. Create;
Tempjpeg: = tsf-image. Create;
Tempjpeg. loadfromfile ('C: \ 1.jpg '); // the size of the original file is 148884 bytes.
Tempjpeg. savetostream (tempstream );
Showmessage (floattostr (tempstream. Size); // 148884 bytes
Tempquery. SQL. Text: = 'insert into timage (imagesize, image) values (: Size,: imagedata )';
Tempquery. Parameters. parambyname ('SIZE'). Value: = tempstream. size;
Tempquery. Parameters. parambyname ('imagedata'). loadfromstream (tempstream, ftgraphic );
Tempquery. execsql;
End;

Procedure tformdemo. bexportclick (Sender: tobject );
VaR
Tempjpeg: tsf-image;
Tempstream: tmemorystream;
Begin
Tempstream: = tmemorystream. Create;
Tempquery. SQL. Text: = 'select image from timage where imageid = 1 ';
Tempquery. open;
Tblobfield (tempquery. fieldbyname ('image'). savetostream (tempstream );
Showmessage (floattostr (tempstream. Size); // 148884 bytes
End;

In addition, I tested the xxmmmx recommendation.

Write
Tempquery. Parameters. parambyname ('imagedata'). loadfromfile ('C: \ 1. jpg ', ftblob );

Read
Tblobfield (tempquery. fieldbyname ('image'). savetofile ('C: \ 2. jpg ');

The results are exactly the same. ('C: \ 1. jpg 'and 'C: \ 2. jpg' are exactly the same)

You can use tclientdataset.
I use tclientdataset.

Is tclientdataset not supported? Tclientdataset is used in the project. Dizzy.

It's okay if I use it like this.
Ado + datasetprovider + tclientdataset

For example:
Save
If cdserfiles. isempty then
Begin
Cdserfiles. append;
Cdserfiles. fieldbyname ('recid'). asstring: = trim (owexaminerepairid. Text );
Cdserfiles. fieldbyname ('state'). asinteger: = 0;
Cdserfiles. fieldbyname ('type'). asinteger: = 0;
End else
Cdserfiles. Edit;
Cdserfiles. fieldbyname ('filename'). asstring: = extractfilename (opendialog. filename );
Tblobfield (cdserfiles. fieldbyname ('filecontent'). loadfromfile (opendialog. filename );

Cdserfiles. applyupdates (0 );

Read and save as a file:
Sfilename: = sfilename + '\' + cdserfiles. fieldbyname ('filename'). asstring;

Tblobfield (cdserfiles. fieldbyname ('filecontent'). savetofile (sfilename );
If fileexists (sfilename) then
ShellExecute (handle, 'open', pchar (sfilename), nil, nil, sw_shownormal)

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.