Delphi将图像插入数据库firebird

时间:2014-09-01 07:56:25

标签: delphi delphi-xe2

我有许多领域的表(员工)。

名字,姓氏,中间名,图像等。

我正在使用dbExpress(TclientDataset)并在事件发布后有代码

 clientdataset1.applyupdates(0)

它有效,但我想插入/更新图像,但它不会将图像保存到数据库(火鸟)

在谷歌搜索ID,但它不适合我想要的,请帮助谢谢

1 个答案:

答案 0 :(得分:0)

以下是我已经整理好的项目的来源和DFM,看看我是否遇到了与您相同的问题。 我没有。它成功加载并将.BMP文件保存到CDS1Image字段。

您没有说出您的列类型,但在我的FB db中,Image列被定义为BLOB。

顺便说一下,我不确定您尝试使用的是哪种图像,但TDBImage无法处理JPEG的长期问题。

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