0
votes

i have table (employee) with many fields.

first name, last name, middle name, image and so on.

i'm using dbExpress (TclientDataset) and have code in the event After Post

 clientdataset1.applyupdates(0)

and it works but i want to insert/update also the image but it doesn't save the image to database(Fire bird)

id search in google but it doesn't fit to what i want, please help thanks

1
What field type are you trying to use when saving image into database. When working with databases and saving any binary data (images, files) the most comonly used field type is Blob field type. - SilverWarior
yes i use blob fieldtype for image - chaco

1 Answers

0
votes

Below is the source and DFM of a project I've put together to see if I get the same problem as you. I don't. It successfully loads and saves .BMP files to the CDS1Image field.

You didn't say what your column type is, but in my FB db, the Image column is defined as a BLOB.

Btw, I'm not sure what kind of image you're trying to work with, but there is a long-standing problem that TDBImage doesn't handle JPEGs.

procedure TForm2.GetImage;
var
  ImageFN : String;
  MS : TMemoryStream;
begin
  if OpenDialog1.Execute then begin
    ImageFN := OpenDialog1.FileName;
  end;

  MS := TMemoryStream.Create;
  MS.LoadFromFile(ImageFN);
  MS.Seek(0, soBeginning);
  try
    CDS1.Edit;
    CDS1Image.LoadFromStream(MS);
    CDS1.Post;
  finally
   MS.Free;
  end;
end;

procedure TForm2.RefreshCDS;
begin
  CDS1.ApplyUpdates(0);
  CDS1.Close;
  CDS1.Open;
end;

procedure TForm2.CDS1NewRecord(DataSet: TDataSet);
var
  ID : Integer;
begin
  Inc(ID);
  if SqlQuery2.Active then
    SqlQuery2.Close;
  SqlQuery2.Open;
  ID := 1 + SqlQuery2.Fields[0].AsInteger;

  CDS1.FieldByName('ID').AsInteger := ID;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  CDS1.Open;
end;

procedure TForm2.btnGetImageClick(Sender: TObject);
begin
  GetImage;
end;

procedure TForm2.btnRefreshClick(Sender: TObject);
begin
  RefreshCDS;
end;

DFM

object Form2: TForm2
  Left = 256
  Top = 95
  Caption = 'Form2'
  ClientHeight = 303
  ClientWidth = 452
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 8
    Top = 8
    Width = 320
    Height = 120
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
  end
  object DBNavigator1: TDBNavigator
    Left = 24
    Top = 144
    Width = 240
    Height = 25
    DataSource = DataSource1
    TabOrder = 1
  end
  object btnRefresh: TButton
    Left = 350
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Refresh'
    TabOrder = 2
    OnClick = btnRefreshClick
  end
  object DBImage1: TDBImage
    Left = 128
    Top = 175
    Width = 105
    Height = 105
    DataField = 'IMAGE'
    DataSource = DataSource1
    TabOrder = 3
  end
  object btnGetImage: TButton
    Left = 350
    Top = 64
    Width = 75
    Height = 25
    Caption = 'GetImage'
    TabOrder = 4
    OnClick = btnGetImageClick
  end
  object SQLConnection1: TSQLConnection
    DriverName = 'Firebird'
    LoginPrompt = False
    Params.Strings = (
      'DriverUnit=Data.DBXFirebird'

        'DriverPackageLoader=TDBXDynalinkDriverLoader,DbxCommonDriver180.' +
        'bpl'

        'DriverAssemblyLoader=Borland.Data.TDBXDynalinkDriverLoader,Borla' +
        'nd.Data.DbxCommonDriver,Version=18.0.0.0,Culture=neutral,PublicK' +
        'eyToken=91d62ebb5b0d1b1b'

        'MetaDataPackageLoader=TDBXFirebirdMetaDataCommandFactory,DbxFire' +
        'birdDriver180.bpl'

        'MetaDataAssemblyLoader=Borland.Data.TDBXFirebirdMetaDataCommandF' +
        'actory,Borland.Data.DbxFirebirdDriver,Version=18.0.0.0,Culture=n' +
        'eutral,PublicKeyToken=91d62ebb5b0d1b1b'
      'GetDriverFunc=getSQLDriverINTERBASE'
      'LibraryName=dbxfb.dll'
      'LibraryNameOsx=libsqlfb.dylib'
      'VendorLib=fbclient.dll'
      'VendorLibWin64=fbclient.dll'
      'VendorLibOsx=/Library/Frameworks/Firebird.framework/Firebird'
      'Database=d:\delphi\firebird\databases\employee.fdb'
      'User_Name=sysdba'
      'Password=masterkey'
      'Role=RoleName'
      'MaxBlobSize=-1'
      'LocaleCode=0000'
      'IsolationLevel=ReadCommitted'
      'SQLDialect=3'
      'CommitRetain=False'
      'WaitOnLocks=True'
      'TrimChar=False'
      'BlobSize=-1'
      'ErrorResourceFile='
      'RoleName=RoleName'
      'ServerCharSet='
      'Trim Char=False')
    Connected = True
    Left = 40
    Top = 24
  end
  object SQLQuery1: TSQLQuery
    MaxBlobSize = 1
    Params = <>
    SQLConnection = SQLConnection1
    Left = 112
    Top = 24
  end
  object DataSource1: TDataSource
    DataSet = CDS1
    Left = 272
    Top = 88
  end
  object CDS1: TClientDataSet
    Active = True
    Aggregates = <>
    CommandText = 'select * from maimages'
    Params = <>
    ProviderName = 'DataSetProvider1'
    AfterOpen = CDS1AfterOpen
    OnNewRecord = CDS1NewRecord
    Left = 280
    Top = 24
    object CDS1ID: TIntegerField
      FieldName = 'ID'
      Required = True
    end
    object CDS1NAME: TStringField
      FieldName = 'NAME'
      Size = 50
    end
    object CDS1IMAGE: TBlobField
      FieldName = 'IMAGE'
      Size = 1
    end
  end
  object DataSetProvider1: TDataSetProvider
    DataSet = SQLQuery1
    Options = [poAllowCommandText, poUseQuoteChar]
    Left = 184
    Top = 24
  end
  object OpenDialog1: TOpenDialog
    Filter = 'BMPs|*.Bmp'
    Left = 400
    Top = 32
  end
  object SQLQuery2: TSQLQuery
    Active = True
    MaxBlobSize = 1
    Params = <>
    SQL.Strings = (
      'select max(ID) from maimages')
    SQLConnection = SQLConnection1
    Left = 16
    Top = 120
  end
end