-5

How can I insert image into SQL Server 2000 table and view that image
using TDBImage control or print it with Fast-Report in delphi?
The below solution working only with SQL Server 2005+

SELECT * FROM OPENROWSET(BULK N'C:\TestImage.jpg', SINGLE_BLOB)
CodeGear
  • 1
  • 1

2 Answers2

0

How to insert image into database?

Have an IMAGE type field (should be available in SQL Server 2000):

CREATE TABLE MyTable (
   ID INT PRIMARY KEY,
   Picture IMAGE
)

And insert an image into it:

FDQuery1.SQL.Text := 'INSERT INTO MyTable (ID, Picture) VALUES (:ID, :Picture)';
FDQuery1.ParamByName('ID').AsInteger := 1;
FDQuery1.ParamByName('Picture').AsStream := TFileStream.Create('C:\Image.jpg', fmOpenRead);
FDQuery1.ExecSQL;

The stream created above will be released after query unpreparing, or after next value assignment.

How to display image in a TDBImage control?

Drop TDBImage control on your form with TDataSource and TFDQuery, for example. Now connect these components and setup source field for the image:

DataSource1.DataSet := FDQuery1;
DBImage1.DataSource := DataSource1;
DBImage1.DataField := 'Picture';

And query for data, for example:

FDQuery1.SQL.Text := 'SELECT Picture FROM MyTable WHERE ID = :ID';
FDQuery1.ParamByName('ID').AsInteger := 1;
FDQuery1.Open;
Victoria
  • 7,822
  • 2
  • 21
  • 44
  • thank you, but the insert statment should from sql itself not from Delphi, is it possible? – CodeGear Jul 15 '17 at 21:13
  • 1
    You're welcome! I'm a bit lost why is this question tagged Delphi and FireDAC, but well, give [this script](https://stackoverflow.com/a/1214485/8041231) a try. You can write a stored procedure based on it. – Victoria Jul 20 '17 at 06:02
-2

Inserting image into table using TADOQuery;

Create Table new_table(
var1...,
var2...,
.,
.,
.,
image1 as type image ,
)

Add the following to your user interface: Jpeg (Sometimes not added by default)

Add the following objects to your project:

  • TOpenPictureDialog (oppCompanyLogo)
  • TImage (imgCompanyLogo)
  • TButton

Event for the button as follows:

if oppCompanyLogo.Execute then
begin
  imgCompanyLogo.Picture.LoadFromFile(oppCompanyLogo.FileName);
end;
  • Image is now assigned to TIamge (imgCompanyLogo) object;
  • Add

    var JPG : TJPEGImage`
    JPG := TJPEGImage.Create;
    JPG.Assign(imgCompanyLogo.Picture);
    
  • Image is now assigned to Jpeg image JPG,

Now we going to simply add the image to the table using TADOQuery:

  • Assumption - You already created your connection to the db (to setup this please create new post)

    with TADOQuery_name do
      begin
         Close;
         Tablename := tablename1;
         Open;
         Insert;
         FieldByName('Var1')...;
         FieldByName('Var2')...;
         .
         .
         .
         FieldByName('Image1').Assign(JPG);
         Post;
         Close;
       End;
    

Simplicity...

RobC
  • 22,977
  • 20
  • 73
  • 80