将RTF文本从数据库加载到TRichEdit [英] Loading RTF text from database into TRichEdit

查看:304
本文介绍了将RTF文本从数据库加载到TRichEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将我们的软件解决方案从Delphi 7迁移到2010年。大部分变化很简单,只剩下少量障碍了。



在表单上,​​我们使用TRichEdit,它显示了从MSSQL数据库中的blob字段中获取的rtf文本。这是它在Delphi 7中的工作原理:

  //使用TADOQuery从Blob字段获取RTF文本
rtfStream:= sql.CreateBlobStream(sql.FieldByName('rtftext'),BmRead)作为TMemoryStream;

//加载到TRichEdit
RichEdit.PlainText:= False;
RichEdit.Lines.LoadFromStream(rtfStream);

这将显示TRichEdit组件中的RTF,但Delphi 2010中的相同代码显示RTF作为每个字符之间的标签的纯文本。我认为这与从Ansi到Unicode的变化有很大关系,但是我没有任何运气纠正这个问题。



非常感谢谢谢

解决方案

好吧,我想出来。



要加载rtf文本:

  //从数据库中获取数据为AnsiString 
rtfString: = sql.FieldByName('rtftext')。AsAnsiString;

//将字符串写入流
stream:= TMemoryStream.Create;
stream.Clear;
stream.Write(PAnsiChar(rtfString)^,Length(rtfString));
stream.Position:= 0;

//将流加载到RichEdit
RichEdit.PlainText:= False;
RichEdit.Lines.LoadFromStream(stream);

stream.Free;

为了保存rtf文本:

  //保存到流
stream:= TMemoryStream.Create;
stream.Clear;

RichEdit.Lines.SaveToStream(stream);
stream.Position:= 0;

//从流中读取到一个AnsiString(rtfString)
if(stream.Size> 0)然后开始
SetLength(rtfString,stream.Size);
if(stream.Read(rtfString [1],stream.Size)< = 0)then
raise EStreamError.CreateFmt('End of stream reached with%d bytes left to read。', stream.Size]);
结束

stream.Free;

//保存到数据库
sql.FieldByName('rtftext')。AsAnsiString:= rtfString;

这让我太久了,找出:)我想我已经学了一件事如果Delphi 2010出现问题,它通常与unicode相关;)


I am currently in the process of migrating our software solution from Delphi 7 to 2010. Mostly the changes have been simple and there are only a small amount of hurdles left to go.

On a form we use a TRichEdit which displayed rtf text grabbed from a blob field in a MSSQL db. This is how it worked in Delphi 7:

//Get RTF text from Blob field using TADOQuery
rtfStream := sql.CreateBlobStream(sql.FieldByName('rtftext'), BmRead) as TMemoryStream;

//Load into TRichEdit
RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(rtfStream);

This would display the RTF as expected in the TRichEdit component, but the same code in Delphi 2010 displays the RTF as plain text with tabs between each character. I assume this has a lot to do with the change from Ansi to Unicode, but I haven't had any luck rectifying the problem.

Any help getting this to work would be much appreciated. Thanks

解决方案

Okay I figured it out.

For loading the rtf text:

//Get the data from the database as AnsiString
rtfString := sql.FieldByName('rtftext').AsAnsiString;

//Write the string into a stream
stream := TMemoryStream.Create;
stream.Clear;
stream.Write(PAnsiChar(rtfString)^, Length(rtfString));
stream.Position := 0;

//Load the stream into the RichEdit
RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(stream);

stream.Free;

For saving the rtf text:

//Save to stream
stream := TMemoryStream.Create;
stream.Clear;

RichEdit.Lines.SaveToStream(stream);
stream.Position := 0;

//Read from the stream into an AnsiString (rtfString)
if (stream.Size > 0) then begin
    SetLength(rtfString, stream.Size);
    if (stream.Read(rtfString[1], stream.Size) <= 0) then
        raise EStreamError.CreateFmt('End of stream reached with %d bytes left to read.', [stream.Size]);
end;

stream.Free;

//Save to database
sql.FieldByName('rtftext').AsAnsiString := rtfString;

This took me way too long to figure out :) I guess I have learned one thing though... if something goes wrong in Delphi 2010, its usually related to unicode ;)

这篇关于将RTF文本从数据库加载到TRichEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆