Link to home
Start Free TrialLog in
Avatar of alim_maze
alim_mazeFlag for Sudan

asked on

Upload/Download picture from/to SQL server DB

Can you please give a code to Upload and download pictures from any type (.bmp,Jpg ..) to MS-SQL Database and then retrieve it back ?
I made simple code to upload -attached - but this is only useful for bitmap
the answer should include the following parts:
1)- Upload Delphi Code on button event + MS-SQL stored procedure
2)- Download Delphi Code on dbgrid.cellclick + MS-SQL procedure

regards,
Alim
procedure TForm2.Button1Click(Sender: TObject);
begin
  sp.close;
  sp.ProcedureName:='upload_img';
  sp.Parameters.Refresh;
  sp.Parameters.ParamByName('@img').LoadFromFile(
                      OpenPictureDialog1.FileName,ftGraphic);
  sp.ExecProc;
end;
 
 
 
Stored Procedure :
CREATE PROCEDURE [dbo].[upload_img]
	@img image
AS
BEGIN
		insert into images (img) values (@img)
	
END

Open in new window

SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have never seen it done through parameters before.  I tend to set up a TADOQuery, get my record results, typecast a BLOB or IMAGE type field as a TBlobField in Delphi, then use the LoadFromFile or SaveToFile methods.
Avatar of alim_maze

ASKER

dear developmentguru,
would you write some code explaining your ideas ..
all I need is your best way to upload and download pictures (of several types) to and from SQL database written with some sample code.
I'll repeat the requirements of this question:
1)- simple Delphi code (procedure/function)  to upload a picture (gif,jpg,bmp ..) to MsSQL DB using ADO (query/Stored procedure .. whatever)
2)- simple Delphi code (procedure/function) to download a picture (gif,bmp,jpg ..) from SQL DB to image1 VCL on form of my application.

I raised the point of this question from 125 to 250,
now please would some body collect these easy points ?!

regards,
Alim
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I now have great result uploading/downloading , the only thing I need now is: how to load the picture from DB to image VCL directly (no files):

Proc
var
img:TBlobField;
begin
  img:=TBlobField(ADOTable1.FieldByName('img'));
  img.SaveToFile(ChangeFileExt(paramstr(0),'.jpg'));
// image1.picture. ???
end;
I saw your line Image1.Picture.Assign(TGIFImage(fGIFs[I]));
but I need something more easier to load from Tadoquery, for example:

var
img:TBlobField;
begin
  img:=TBlobField(ADOTable1.FieldByName('img'));
 image1.picture ...
You will need to:
1)  save the blob field to an in memory stream
2) Create a TGIFImage
3) Load the GIF Image from the in memory stream
4) Do the Image1.Picture.Assign to the TGIFImage

You can wrap all of that in a method or procedure if you want, but it is all necessary.
thank you ..
my best and fastest way to solve this case (with the help of DevExpress cxImage) is as follows:


// 1)- Upload picture to DB:
  sp.close;//sp: is TADOStoredProcedure
  sp.ProcedureName:='upload_img';
  sp.Parameters.Refresh;
  sp.Parameters.ParamByName('@img').LoadFromFile(
  sp.Parameters.ParamByName('@ext').value:=(
                 extractfileext(
                 OpenPictureDialog1.FileName,ftGraphic));
  sp.ExecProc;
(or I can use AdoQuery)
 
// 2)- Download picture from DB:
var
img:TBlobField;
fname:string;
begin
     img:=TBlobField(ADOTable1.FieldByName('img'));
     fname:=ChangeFileExt(paramstr(0),ADOTable1.FieldByName('ext'));
     img.SaveToFile(fname);
 
     cxImage1.Picture.LoadFromFile(fname);
 
end;

Open in new window