How to insert image into database using TADOQuery Component Only
Saving:
var
Field: TBlobField;
Stream: TStream;
begin
if ADOQuery.Active and (Image.Picture.Graphic <> nil) then
begin
ADOQuery.Insert;
Field := TBlobField(ADOQuery.FieldByName('ImageData')); // ensure it ís a blob
Stream := ADOQuery.CreateBlobStream(Field, bmWrite);
try
Image1.Picture.Graphic.SaveToStream(Stream);
finally
Stream.Free;
ADOQuery.Post;
end;
end;
end;
or use a TADOBlobStream
instead of a TStream
:
var
...
Stream: TADOBlobStream;
begin
...
Stream := TADOBlobStream.Create(Field, bmWrite);
...
Loading:
var
Field: TBlobField;
Stream: TStream;
Jpg: TJPEGImage;
begin
if ADOQuery.Active then
begin
Field := TBlobField(ADOQuery.FieldByName('ImageData'));
Stream := ADOQuery.CreateBlobStream(Field, bmRead);
Jpg := TJPEGImage.Create;
try
Jpg.LoadFromStream(Stream);
Image1.Picture.Graphic := Jpg;
finally
Jpg.Free;
Stream.Free;
end;
end;
end;
When working with a parameter as you do, I think you have to provide it with additional settings like Attributes
and DataType
, as follows:
sSql := 'INSERT INTO Table_Name (Column1, Column2, Column_Image) ' +
'VALUES (''' + Value1 + ''', ''' + Value2 + ''', :pBlob)';
qryADOQuery.SQL.Add(sSQL);
qryADOQuery.Parameters[0].Attributes := [paLong];
qryADOQuery.Parameters[0].DataType := ftBlob; // Or ftVarBytes
// Or ftOraBlob (Oracle only)
qryADOQuery.Parameters[0].LoadFromStream(msBinImgStream, ftBlob);
qryADOQuery.ExecSQL;