I am using Delphi XE2. My code retrieves data from a SQL-Server 2008 R2 database. The returned data is an nvarchar(max) field with 1,055,227 bytes of data. I use the following code to save field data to a file:
procedure WriteFieldToFile(FieldName: string; Query: TSQLQuery); var ss: TStringStream; begin ss := TStringStream.Create; try ss.WriteString(Query.FieldByName(FieldName).AsString); ss.Position := 0; ss.SaveToFile('C:\Test.txt'); finally FreeAndNil(ss); end; end;
When I check the file in hexadecimal view, the first 524,287 bytes (exactly 1/2 megabytes) look correct. The remaining bytes (524,288 to 1,055,227) are all zeros (# 0) instead of the original data.
Is this the right way to save a string field from TSQLQuery to a file? I decided to use TStringStream because in the end I will add code to do other things for the data in the stream that I cannot do with TFileStream .
source share