我如何将用Query查询到的数据写成Txt文件

解决方案 »

  1.   

    var
      F:File;
      i:integer;
    begin
      while not Query.RecordSet.Eof do
      begin
        for i:=0 to Query.RecordCount-1 do
           Writeln(F,Query.RecordSet.Fields[i].Value);
        Query.RecordSet.Next;   
      end;
      

  2.   

    转变成txt文件,这样就可以了
    procedure TForm14.DataSetToASCII(const ADataSet: TDataSet; const ASCIIFile: TFileName; const Delimiter: Char; const QuoteStrings: Boolean);var tmpList: TStringList;
          i,LastIndex: LongInt;
          AsciiRecord: String;
      begin
        tmpList:= TStringList.Create;
           try
              with ADataSet do begin
            LastIndex:= Fields.Count - 1; 
                First;
              while not EOF do begin
              AsciiRecord:= '';
                   for i := 0 to LastIndex do 
                if Fields.Fields[i].Tag = 0 then begin 
      
                  if QuoteStrings and 
                     (Fields.Fields[i].DataType in [ftString,ftMemo,ftFmtMemo,ftFixedChar,ftWideString]) then 
                    AsciiRecord:= AsciiRecord + QuotedStr(Fields.Fields[i].AsString) 
                  else 
                    AsciiRecord:= AsciiRecord + Fields.Fields[i].AsString; 
      
                  if i < LastIndex then 
                    AsciiRecord:= AsciiRecord + Delimiter; 
      
                end; 
      
              tmpList.Append(AsciiRecord); 
      
              Next 
            end 
          end; 
      
          try
            tmpList.SaveToFile(ASCIIFile+'.txt') ;
          except 
            ShowMessage('Could not save table to specified file: ' + ASCIIFile) 
          end; 
      
        finally 
          tmpList.Free; 
        end; 
      end;