Access technology for image data in database applications
Delphi provides visual controls for Data Access (DataAccess) and Data Control (DataControls), which can easily and easily have a good interface and powerful database application. For database applications involving image data (including graphic fields), such as personnel management information systems, etc., access technology of image data is a key. However, there are few information about the access, especially the image of the image below Delphi. Below, the author combines a simple example to illustrate.
First, the preservation of image data
1. Create a list of databases with a graphic field.
Create Database mydb.gdb under Windowsisq (or DatabaseDesktop)
Create Table Myfrieds (Name Varchar (15) Notnull, Telephone Varchar (12), Address Varchar (30), ZipVarchar (8), Picture Varchar (15), Image Blob);
Where the Picture field is used to save the name of the image (including path), the image (graphic character) is used to store the image, and its data type is "blob".
2. Establish a form (as shown in Figure 1), set the properties of each control in the form.
The main function of the form is to edit and save someone information. The image boxes you need to pay attention to the image boxes used must not be used with TDBIMAGE, the edit box should be used with TEDIT without using TDBEDIT, which is just the opposite of the image.
Among them, the properties of each major control are as follows:
DataSource1.DataSet: = Table1;
Table1.DatabaseName: = mydb.gdb;
Table1.tablename: = myfriends;
Table1.Active: = true;
Other property settings such as CAPTION are no longer described.
3. The establishment of the data handler.
(1) The processing of the image (.bmp file) is opened.
Proceduretform1.pictopenbtnclick (sender: TOBJECT);
Beginopendialog1.execute;
Image1.Picture.loadFromFile (OpenDialog1.FileName);
END;
(2) Processing of image saving.
The image saved handler completes the information edited by the form 1 including the image to the corresponding database, the key is to define a variable of a Graphic type and the variable is passed to the corresponding database to save. The specific procedures are as follows:
Proceduretform1.savebtnclick (sender: TOBJECT);
VAR
Graphic1: tgraphic;
Begin
Graphic1: = tgraphic.create;
Graphic1.loadFromfile (OpenDialog1.FileName);
Table1.insert;
Table1.fieldbyName ('name'). asstring: = edit1.text;
Table1.fieldbyname ('Telephon'). asstring: = edit2.text;
Table1.fieldbyName ('address'). Asfloat: = Edit3.Text;
Table1.fieldbyname ('zip'). Asfloat: = Edit4.Text;
Table1.fields [4] .ssign (graphic1);
Table1.post;
Graphic1.free;
END;
Second, the reading of image data
In terms of database data (including data for graphic field), the superiority of Delphi's object-oriented development tools with powerful code automatically generates the superiority. If you don't exaggerate, you don't need a statement! The settings of the properties of Table1 and DataSource1 are the same as the save section of the data (Fig. 1), and the database data is read with TDBEDIT and TDBIMAGE controls without TEDIT and TIMAGE. Controls TDBEDIT and TDBIMAGE As long as the DataField property is set to its corresponding domain; the TDBNAVIGATOR's DataSouce property is set to DataSource1.
TRE
Level: Soldier
From: Xiamen
OICQ:
Points: 457
Expert points: 107
Total ranking: 180
Finally, appeared: 2001-5-23 19:34:05 (Landing 100 times)
Published in: 2001-5-23 20:02:40
-------------------------------------------------- ------------------------------
I also picked you on the ADO book of Li Wei.
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
VAR
Sfilename: String;
Function blobcont (const filename: string): string;
Begin
With TfileStream.create (FileName, Fmopenread) Do
Try
SetLENGTH (RESULT, SIZE);
Read (Pointer (Result);
Finally
FREE;
END;
END;
Begin
IF openDialog1.execute the
Begin
SfileName: = Opendialog1.FileName;
Adodataset1.append;
AdoDataSet1.fieldByname ('ff'). asstring: = blobcont (sfilename);
AdoDataSet1.post;
END;
END;
Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);
VAR
Sfilename: String;
BS: tadoblobstream;
Begin
BS: = TadoblobStream.create (TBLOBFIELD (AdoDataSet1.fieldbyName ('FF')), BMREAD);
Try
sfilename: = 'c: /ff.doc'; // can be replaced with a picture format
IF fileexists (sfilename) THEN
Deletefile (sfilename);
Bs.savetofile (sfilename);
OleContainer1.createObjectFromfile (sfilename, false);
//Olecontainer1.loadfromfile(SFileName);
//Olecontainer1.loadfromstream ()
//Memo1.Lines.LoadFromfile (SFileName);
// WebBrowser1.naviGate (pchar (sfilename));
Finally
BS.Free;
END;