Have a friend's first school programming, try to be a simple person system, asked if I asked me if I implemented the photo, write operations in the program. I have done before this problem, but the time is long, only remembers that I have studied it, and I have implemented it and use the memory stream. It is realized relative to the length, complexity of the online other implementation methods. It is simple and efficient.
We know that BLOB is actually stored in the form of binary data in the table of the database. Due to the particularity of blob, the general procedures cannot process it. For example, if there is blob in a table, when you open it with a data display control, the blob column will only display the BLOB word. As for what data in this column is, what data is a general data display control that cannot be known. If you have a program that we prepared, open a table with a Blob field with a DBGRID control, the effect is the same. Our program cannot display, edit, and insert the blob field. It can be seen that the conventional method cannot meet the requirements.
Since we know that the data is stored in binary form, you can complete it through memory flow. With memory flow, the disk operation can be reduced, thereby greatly improving operational efficiency. The implementation code of the program is as follows (described in Delphi as an example):
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
//
Insert a picture process
Var TestStream: TmemoryStream; Begin
Try
TestStream:
=
TmemoryStream.create;
//
Create memory flow
Image1.picture.graphic.savetostream (TestStream);
//
Save pictures to the memory stream
Adoquery1.close; adoQuery1.sql.clear; adoquery1.sql.add (
'
INSERT INTO TEST (ID, Photo) Values (: ID,: photo)
'
);
//
Insert operation
AdoQuery1.Parameters.Parambyname (
'
id
'
) .Value:
=
'
003
'
AdoQuery1.Parameters.Parambyname (
'
Photo
'
) .Loadfromstream (TestStream, ftblob);
//
Read saved memory diagram
AdoQuery1.execsql;
Finally
TestStream.free;
//
Release memory flow
End; End; Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);
//
Read the picture process
Var MStream: TmemoryStream; JPGFile: TjpegImage; Begin
IF
NOT AdoQuery1.fieldbyName
'
Photo
'
) .NULL THEN BEGIN; MSTREAM:
=
TmemoryStream.create; JPGFile:
=
TJPEGIMAGE.CREATE; TBLOBFIELD (AdoQuery1.fieldByname
'
Photo
'
)). SaveTroupTream (MSTREAM);
//
Displayed to blobfiled and saved to memory stream
MStream.position:
=
0
JPGFile.LoadFromstream (MSTREAM); image2.picture.assign (jpgfile); end
Else
Begin image2.picture:
= NIL; END;
The above examples only explain the read and write operation process of the picture, of course, as long as small changes, you can become a read and write any BLOB type.
The content on the Internet is too rich, if there is a similar, purely a coincidence!
Article source:
http://www.cnblogs.com bonny.wong (let ideas flying) on 2005.1.25