FishFacts (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example copies the data in the Graphic field of Table1 or SQLDataSet1 to the Pictures field of ClientDataSet1.

Code

var
  myBlobField : TBlobField;

procedure TForm1.CopyDataSourceClick(Sender: TObject);
var
  Stream1: TBlobStream;
  Stream2: TStream;
  blobType : TBlobType;
begin
  blobType := myBlobField.BlobType;
  DBImage2.DataField := 'Pictures';
  Stream1 := TBlobStream.Create(myBlobField, bmRead);
  try
    CDS2.Edit;
    { This is a different way to create a blob stream. }
    Stream2 := CDS2.CreateBlobStream(CDS2.FieldByName('Pictures'), bmReadWrite);
    try
      Stream2.CopyFrom(Stream1, Stream1.Size);
//    CDS2.Post;
//    CDS2.Active := True;
    finally
      Stream2.Free;
    end;
  finally
    Stream1.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I : Integer;
  S : String;
  ft : TFieldType;
  myString: String;
begin
// These properties can also be set statically at design time.
  Table1.DatabaseName := 'DBDEMOS';
  Table1.TableName := 'BioLife.DB';
  DataSource1.DataSet := Table1;
  DBGrid1.DataSource := DataSource1;
  Table1.Active := True; // Do this last
  myBlobField := nil;
  for I := 0 to Table1.FieldCount - 1 do
  begin
    S := Table1.Fields[I].FieldName;
    ft := Table1.Fields[I].DataType;
    case ft of
      ftFloat: myString := 'float';
      ftBoolean: myString := 'boolean';
      ftInteger: myString := 'integer';
      ftString: myString := 'string';
      ftCurrency: myString := 'currency';
      ftBlob:
      begin
        myString := 'blob';
        if (Table1.Fields[I] is TBlobField) then
          myBlobField := TBlobField(Table1.Fields[I]);
      end;
      ftSmallint: myString := 'smallint';
      ftTime: myString := 'time';
      ftDate: myString := 'date';
      ftDateTime: myString := 'datetime';
      ftTypedBinary: myString := 'typedBinary';
      ftMemo: myString := 'memo';
      ftGraphic: myString := 'graphic';
      ftAutoInc: myString := 'autoinc';
      else myString := 'unknown';
    end;
    ListBox1.Items.Add('Field ' + S + ' is of type: ' + myString);
  end;
  if (myBlobField <> nil) then
  begin
    with CDS2 do
    begin
      with FieldDefs.AddFieldDef do
      begin
        DataType := ftInteger;
        Name := 'Field1';
      end;
      with FieldDefs.AddFieldDef do
      begin
        DataType := ftBlob;
        Size := 10;
        Name := 'Pictures';
      end;
      with IndexDefs.AddIndexDef do
      begin
        Fields := 'Field1';
        Name := 'IntIndex';
      end;
      CreateDataSet;
//      DBMemo2.DataField := 'Remarks';
    end;
  end;
end;

Uses

See Also