Solved

i want ask about database in delphi 5.0

Posted on 2001-07-04
16
288 Views
Last Modified: 2010-04-06
hi...

i want make database about field (blob image), i want save my picture(*.jpg) in my field (BLOB Image)...
and update my picture in this field...


please help me...
0
Comment
Question by:Rudy_Rai
  • 5
  • 5
  • 3
  • +2
16 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
for what database?

(i'm sure epsylon will give you a working sample,
he has delphi on hand, which i don't have yet)

meikl ;-)
0
 
LVL 22

Accepted Solution

by:
mnasman earned 150 total points
Comment Utility
i'm working with one project, it work with blob filed in access and oracle


unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 DBCtrls, StdCtrls, Mask, Db, DBTables, ExtCtrls, Grids, DBGrids,Jpeg;

type
 TForm1 = class(TForm)
   Button1: TButton;
   Image1: TImage;
   Table1: TTable;
   DataSource1: TDataSource;
   Database1: TDatabase;
   Table1PIC: TBlobField;
   DBNavigator1: TDBNavigator;
   OpenDialog1: TOpenDialog;
   DBGrid1: TDBGrid;
   Table1ID: TStringField;
   Label1: TLabel;
   DBEdit1: TDBEdit;
   procedure Button1Click(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure FormCreate(Sender: TObject);
   procedure DataSource1DataChange(Sender: TObject; Field: TField);
 private
   { Private declarations }
   FileName : String;
   procedure ReadPicFromDB;
 public
   { Public declarations }

 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}
uses shellapi;
procedure TForm1.ReadPicFromDB;
var
 FileStream : TFileStream;
 BlobStream : TBlobStream;
begin
 Image1.Picture.Assign(nil);
 BlobStream := TBlobStream.Create(Table1Pic, bmRead);
 if BlobStream.Size =0 then
 begin
   BlobStream.Free;
   Exit;
 end;
 if FileName <> '' then
   DeleteFile(FileName);
 FileName := 'Img.jpg';
 FileStream := TFileStream.Create(FileName, fmCreate or fmOpenWrite);
 FileStream.CopyFrom(BlobStream,BlobStream.Size);
 FileStream.Free;
 BlobStream.Free;
 Image1.Picture.LoadFromFile(FileName);
end;// end procedure radpicfromdb

procedure TForm1.Button1Click(Sender: TObject);
var
 FileStream : TFileStream;
 BlobStream : TBlobStream;
begin
 Application.ProcessMessages;
 if OpenDialog1.Execute then
 begin
   Button1.Enabled := false;
   FileStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
   Table1.Edit;
   BlobStream := TBlobStream.Create(Table1Pic,bmReadWrite);
   BlobStream.Seek(0,soFromBeginning);
   BlobStream.Truncate;
   BlobStream.CopyFrom(FileStream,FileStream.Size);
   FileStream.Free;
   BlobStream.Free;
   Table1.Post;
   ReadPicFromDB;
   Button1.Enabled := True;
 end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 if FileName <> '' then
   DeleteFile(FileName);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 OpenDialog1.Filter := 'Jpeg Files |*.jpg';
 OpenDialog1.Options := [ofHideReadOnly, ofNoChangeDir];
 Image1.Stretch := true;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
 if DataSource1.State = dsBrowse then
   ReadPicFromDb;
end;

end.
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
if you want, send me email at nasman@mogaza.org to send u the project if u like
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
well, mnasman did it
0
 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
My standard answer  :o)

You can store for example jpeg images in the database by using a blob field (Ole Object field in Access):


var fs: TFileStream;
bs: TBlobStream;


Writing to database:

with Table1 do
begin
Append;
fs := TFileStream.Create('image1.jpg', fmOpenRead);
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmWrite));
bs.CopyFrom(fs, 0);
bs.Free;
fs.Free;
Post;
end;


Reading from database:

with Table1 do
begin
fs := TFileStream.Create('image1.jpg', fmOpenWrite or fmCreate);
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmRead));
fs.CopyFrom(bs, 0);
bs.Free;
fs.Free;
end;


If you want to display JPEG images you can use a TJPEGImage and then assign it to a TImage:


var j: TJPEGImage;

with Table1 do
begin
bs := TBlobStream(CreateBlobStream(FieldByName('picture'), bmRead));
j := TJPEGImage.Create;
j.LoadFromStream(bs);
Image1.Picture.Assign(j);
bs.Free;
j.Free;
end;


Hope this helps...

Epsylon.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
:-)))
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
well, then i will post also my old version from my paq

first in the paradox structure the BlobField is type Binary
and now follows a sample-unit,
which stores and get the JPeg

unit db_pict_u;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 Db, DBTables, ExtCtrls, DBCtrls, StdCtrls, Mask, Menus, jpeg; // JPeg unit is used

type
 TForm1 = class(TForm)
   DBEdit1: TDBEdit;   // A other DBField
   DBNavigator1: TDBNavigator;
   Table1: TTable;
   DataSource1: TDataSource;
   PopupMenu1: TPopupMenu;     // a Popup linked to Image1
   LoadPicture1: TMenuItem;    // the MenuItem of the Popup
   OpenDialog1: TOpenDialog;   // OpenDialog
   Image1: TImage;             // Visual Output for the JPeg
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure LoadJPEG1Click(Sender: TObject);      // File Load and Store in DB
   procedure Table1AfterScroll(DataSet: TDataSet); // Get JPeg and Display in Image1
 private
   { Private-Deklarationen }
 public
   { Public-Deklarationen }
 end;

var
 Form1: TForm1;
 JPegImage : TJpegImage;  //A temporary JPeg-Object

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 JpegImage := TJPegImage.Create; // Create JPeg-Object
 Table1.Open;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 Table1.Close;
 JPegImage.Free;  // Free JPeg-Object
end;

procedure TForm1.LoadJPEG1Click(Sender: TObject);
Var
 MyBlobStream : TBlobStream;      // Streams
 MyJPegStream : TMemoryStream;
begin
 if Table1.State in [dsEdit,dsInsert] then    // Do only if in edit or insert mode
 begin
   If opendialog1.Execute then    // If file selected
   begin
     if Table1.FieldByName('Bild2').IsBlob then   // this line can deleted
     begin
       MyJPegStream := TMemoryStream.Create;      // Handle JPegFile
       JPegImage.LoadFromFile(opendialog1.filename);
       JPegImage.SaveToStream(MyJPegStream);
       // Prepare Blob
       MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmReadWrite);
       MyBlobStream.CopyFrom(MyJPegStream,0);  // Copy JPeg into BlobField
       Image1.Picture.Assign(JPegImage);  // Display
       MyJPegStream.Free;  //Free all
       MyBlobStream.Free;
     end;
   end;
 end else ShowMessage('Table is not in Edit Mode!');
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
Var
 MyBlobStream : TBlobStream;
begin
 // Do Only if a JPeg available
 if (Table1.FieldByName('Bild2').IsBlob) and (not(Table1.FieldByName('Bild2').IsNull)) then
 begin
   // Prepare Streams
   MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmRead);
   JPegImage.LoadFromStream(MyBlobStream);
   Image1.Picture.Assign(JPegImage);  //Display JPeg
   MyBlobStream.Free;  // Free Stream
 end
 else image1.Picture := Nil;  // No JPeg saved disable display
end;

end.

meikl ;-)))

0
 

Expert Comment

by:ondertol
Comment Utility
You must Load your image from disk to Table using this istruction:

Table1.Edit;
Table1Field.LoadFromFile('c:\mydir\myImage.jpg');
Table1.Post;

for use this istruction you must insert your table on your form and Add your graphic Fields (double click into Table1 component into your form + left click on the new window + Add Fields).

Table1Field is the name assigned at your graphic field, the first part is your Table name, the second part is your Fields name (you can see it on object into your unit program, the type is TGraphicField). The string "myImage.jpg" is the name of your image (you can insert the directory)!

If you want update this image on your table you must reload the image (using the same istruction).

If you desire save the image on your table into file disk, write this:

Table1Field.SaveToFile('c:\mydir\NewImage.jpg');


For load one file selected by user, can you write this code:

If Open.Execute then
begin
  Table1.Edit;
  Table1Field.LoadFromFile(Open.FileName);
  Table1.Post;
end;

Open is one TOpenDialog component!!!
Modify the Open.Filter property to list only *.jpg file (Open.Filter := 'JPEG Format |*.jpg').

!!! remember: insert the jpeg unit on your program (uses jpeg) !!!!!

bye bye

bye bye


0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
>>>sigh<<<
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Epsylon,  :-/
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
ondertol,
thats not the right way, even as i guess that would not work correctly
and also if it works it needs persistent fields

all comments above your answer, which are containing code are verified working samples,

so please be so kind, and withdraw your answer, and let the questioner decide,
(keep this also in mind for the future, because it was not the first  time you did this!)

if not,
then

Rudy_Rai, please reject ondertol's answer and grade mnasman,
which gives you first a correct answer

meikl ;-)
0
 

Author Comment

by:Rudy_Rai
Comment Utility
sorry ondertol, because i will try first their program ok...
0
 

Author Comment

by:Rudy_Rai
Comment Utility
i want in database paradox (*.db)....
and  halo mnasman what is table1pic and table1id ??
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
it's two fields in the table
i made talbe with two fields to test
one is id and the other is pic
id : string
pic : blob
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
the example i gave u it work fine with paradox table
i test it with oracle, access and paradox, and it work fine with them without any change in code
just change the table name
if you need a copy of this project, give me ur email

Mohammed
0
 

Author Comment

by:Rudy_Rai
Comment Utility
my email is: rudy_r@mailcity.com...

can u give me your source code...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now