在包含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;