After a connection made with the FireDac
, you can use one of your own components to execute the commands SQLs
.
The Component is in the same tab of FireDacconnection
, this is about the FDQuery
.
Saving the image in the Database:
procedure frmTeste.btnIncluirFotoClick(Sender: TObject);
var
vArquivo: TFileStream;
begin
vArquivo := TFileStream.Create('d:\01.bmp', fmOpenRead or fmShareDenyWrite);
FDQuery1.Close;
FDQuery1.SQL.Clear;
//Pode ser feito Insert ou Update
FDQuery1.SQL.Add('INSERT INTO tbl_usuario (FOTO) VALUES(:FOTOCARREGADA)');
FDQuery1.Params[0].DataType := ftBlob;
FDQuery1.ParamByName('FOTOCARREGADA').AsStream := vArquivo;
FDQuery1.ExecSQL;
FDQuery1.Close;
end;
Reading the Database Image:
procedure frmTeste.btnCarregarFotoClick(Sender: TObject);
var
vFoto : TStream;
vImagem : TBitmap;
begin
FDQuery1.Close;
FDQuery1.SQL.Add('SELECT * FROM tbl_usuario WHERE id_usuario = 6');
FDQuery1.Open;
//Lendo a imagem do campo BLOB para a Memória
vFoto := FDQuery1.CreateBlobStream(FDQuery1.FieldByName('FOTO'), bmRead);
//Criando a instância de TBitmap (pode ser JPG tb)
vImagem := TBitmap.Create;
//Carregando a imagem a partir do stream TStream
vImagem.LoadFromStream(vFoto);
//Exibindo a Imagem
Image1.Picture.Assign(vImagem);
//Image1.Bitmap.LoadFromStream(vFoto); //Caso esteja usando FMX ao invés de VCL
end;
To have a success in writing and reading the data, choose the field type Blob must be taken into account!
Follow a question I asked right here with good answers about the field BLOB
When you say, "insert/recover database image" you mean create/restore ?
– Junior Moreira
Yes, Exactly, Insert Image, and Query/Restore BD Image.
– Viniam
Viniam, This video shows the whole process, from the capture by the Image component to the passage of parameter to STP, in Delphi 7, but it is for SQL Server, however, can give a light! Follow the video link: https://youtu.be/8pGGDFG_Kis
– Tito de Barros Junior