将变量数组字节保存到参数化查询

时间:2016-05-31 12:36:56

标签: delphi datasnap

在包含blob字段的ClientDataSet的TDataSetProvider.OnBeforeUpdate事件处理程序中,我想分配DeltaDS.Fields[i].NewValue的值,在这种情况下,当然这是一个变量数组的字节,用于blobField值 - Update语句的Query.Parameter,例如Update MyTable set myBlob = :myBlob where id = :id

显然,我弄得一团糟。

这样做的优雅方式是什么?感谢

    i := 0;
    while (i < DeltaDS.Fields.Count) do
    begin
       // not all the code, just the bit relevant to the question
       if (DeltaDS.Fields[i].DataType = TFieldType.ftBlob) then
       begin
         // field value is an Array of Byte
         // copy it first
         ab := DeltaDS.Fields[i].NewValue; // no error here
         if (Length(ab) * SizeOf(Char)) > 0 then
         begin
           Stream := TMemoryStream.Create;
           try
             // write array of byte to stream
             Stream.WriteBuffer(ab[0], Length(ab) * SizeOf(Char));
             Stream.Position := 0; // rewind
             // load parameter from stream
             QryUpdate.Params.ParamByName(DeltaDS.Fields[i].FieldName).DataType := ftBlob;
             QryUpdate.Params.ParamByName(DeltaDS.Fields[i].FieldName).LoadFromStream(Stream);
           finally
             Stream.Free;
           end;
         end;
       end;
       Inc(i);
     end;

0 个答案:

没有答案