Search code examples
delphitadoquery

Delphi TADOQuery: Format data before SavetoFile


I have an application that allows a user to run a query over a remote database using a TADOQuery component. The query results are then saved to an XML file using the TADOQuery's SaveToFile method.

I've recently run into an issue whereby if a line feed character (hex "0A") exists in one of the fields, the data in that field is truncated (i.e. it's only partially saved to file). Null characters in the field cause similar problems.

What I need to do is scan the field in question before I call the SaveToFile method, and replace any "problem" characters with something else (e.g. a space). I'm not allowed to update the data in the database though, so I can't push any changes back to the database (which means that the TADOQuery can't be in edit mode).

Is there any way that I can achieve this (i.e. editing a field without making changes to the data in the database, after running a SELECT but before calling SaveToFile)? I have a workaround in mind but it's rather long-winded and inefficient, but it would allow me to edit the XML document without touching the data on the server (basically, read the problem field, make the changes and store the updated versions of the fields in a temporary file, call SaveToFile, and then replace the field in the XML document with the updated versions). Is this my best option, or is there a better way?


Solution

  • The TCustomADODataSet.SaveToFile method internally calls to the RecordSet.Save method which not allow any customization in the way how the data is stored. Anyway you can connect your TAdoQuery to an TClientDataset which provides a SaveToFile method, another option is write your own method to save the data to a XML file.