我需要将打包记录保存到我使用ADO连接的数据库中。使用CreateBlobStream使用ADOTable很容易做到这一点,但我需要使用SQL UPDATE / INSERT查询来完成。我找到的最接近的解决方案是this one。但是,与TJpegimage不同,您不能将打包记录分配给参数,因为它不是TPersistent对象。如何才能使此代码生效?
type
TMyRecord = packed record
FontName: string[30];
FontSize: word;
FontColor: integer;
FontStyle: word;
Attachement: string[255];
URL: string[255];
end;
var
MyRec: TMyRecord;
begin
//assume MyRec populated here
DMa.qry1.SQL.Text:= 'INSERT INTO my_table (blob_field) VALUES(:rec_data)';
DMa.qry1.Parameters[0].Assign(MyRec);
DMa.qry1.ExecSQL;
end;
建议的解决方案是将记录保存到流中并将其加载到参数中。然而,这导致标准表达式中的数据类型不匹配"错误。
stream := TMemorystream.Create;
try
Stream.Write(MyRec, SizeOf( MyRec));
DMa.qry1.Parameters[0].LoadFromStream(Stream,ftBlob);
stream.Position := 0;
DMa.qry1.ExecSQL;
finally
Stream.Free;
end;