Question
“How do I manually edit report templates stored in my database as text files?”
Solution
Stream the templates from thier BLOB field using the GetFieldAsStream routine of the DataPipeline.
Use the built-in Delphi routines ObjectBinaryToText and ObjectTextToBinary to load and save each template to and from text.
Once the template has been edited, use the SetFieldFromStream routine of the Datapipeline to write the template information back to the database.
See the example below on how this can be done
Download: EditTemplatesAsText.zip
Sample Delphi code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
{------------------------------------------------------------------------------} { TForm1.LoadTemplate } procedure TForm1.LoadTemplate(aItemIndex: Integer); var liItemID: Integer; lBinaryStream: TMemoryStream; lTextStream: TMemoryStream; begin {get the item id for the selected item} liItemID := Integer(lbxItems.Items.Objects[aItemIndex]); {locate the record to edit} if not plItem.Locate('ItemID', liItemID, []) then raise Exception.Create('Unable to locate selected report'); lBinaryStream := TMemoryStream.Create; lTextStream := TMemoryStream.Create; try {read the binary stream from the database} plItem.GetFieldAsStream('Template', lBinaryStream); lBinaryStream.Position := 0; {convert the binary stream to text} ObjectBinaryToText(lBinaryStream, lTextStream); lTextStream.Position := 0; {load the text stream to the memo control} memo1.Lines.LoadFromStream(lTextStream); finally lBinaryStream.Free; lTextStream.Free; end; end; {------------------------------------------------------------------------------} { TForm1.SaveTemplate } procedure TForm1.SaveTemplate; var lBinaryStream: TMemoryStream; lTextStream: TMemoryStream; lWriter: TWriter; begin lBinaryStream := TMemoryStream.Create; lTextStream := TMemoryStream.Create; try {load the text stream to the memo control} memo1.Lines.SaveToStream(lTextStream); lTextStream.Position := 0; {convert the binary stream to text} ObjectTextToBinary(lTextStream, lBinaryStream); lBinaryStream.Seek(0,soFromEnd); lWriter := TWriter.Create(lBinaryStream, 1024); try lWriter.Root := Self; lWriter.WriteListEnd; finally lWriter.Free; end; lBinaryStream.Position := 0; plItem.Edit; {load the binary stream from the database} plItem.SetFieldFromStream('Template', lBinaryStream); plItem.Post; finally lBinaryStream.Free; lTextStream.Free; end; end; |