写入数据库:
procedure TForm1.Button4Click(Sender: TObject);
var
messagepart:tmemorystream;
begin
if not ad1.Active then
ad1.Open;
msg.Clear;
idpop31.Retrieve(strtoint(edit1.Text),msg);
ad1.Append;
ad1.FieldByName('FromName').AsString:=msg.From.Text;
ad1.FieldByName('FromAddress').AsString:=msg.From.Address;
ad1.FieldByName('ToAddress').AsString:=msg.Recipients.EMailAddresses;
ad1.FieldByName('subject').AsString:=msg.Subject;
ad1.FieldByName('date').AsDateTime:=msg.Date;
messagepart:=tmemorystream.Create;
messagepart.WriteComponent(tcomponent(msg.MessageParts));
messagepart.Seek(0,sofrombeginning);
Tblobfield(ad1.FieldByName('MessageParts')).LoadFromStream(messagepart);
ad1.Post;
end;
从数据库读出:
procedure TForm1.Button5Click(Sender: TObject);
VAR
STRSTREAM:TSTRINGSTREAM;
BINSTREAM:TMEMORYSTREAM;
MESSAGEPART:TIDMESSAGEPARTS;
IT:TLISTITEM;
I:INTEGER;
S:TCOMPONENT;
begin
S:=TCOMPONENT.Create(APPLICATION);
IF NOT AD1.Active THEN
AD1.Open;
STRSTREAM:=TSTRINGSTREAM.Create(AD1.Fields.FieldBYNAME('MESSAGEPARTS').AsString);
BINSTREAM:=TMEMORYSTREAM.Create;
OBJECTTEXTTOBINARY(STRSTREAM,BINSTREAM);
BINSTREAM.Seek(0,SOFROMBEGINNING);
MESSAGEPART:=TIDMESSAGEPARTS.Create(APPLICATION);
BINSTREAM.ReadComponent(S);
MESSAGEPART:=TIDMESSAGEPARTS(S);
end;
写入数据库没问题,读出时出错,提示:OBJECT excepted on line 1.