做一个循环将DBGrid关联的DataSet的值全部取出来放入文本文件中去。

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      AdoQuery1.SaveToFile('Tikkepeng.Txt',pfXML);
      //或者是
      //AdoQuery1.SaveToFile('Tikkepeng.Txt',pfADTG);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      ADOQuery2.Close;
      ADOQuery2.LoadFromFile('Tikkepeng.Txt');
    end;或者是作循环自己向文本文件里面写~
      

  2.   

    先根据数据记录数循环
    For i:=0 to Adoquery1.Recordcount do
    begin
      在根据字段数进行循环
      for j:=0 to ADOQuery1.FieldCount do
      begin
        向文本里面写数据~~
      end;
    end;
      

  3.   

    有好多种方法可以实现,
    如果你用的是ado那么,你可以用
    adoconnection1.excute('select * into [text;database='c:\'].ttt1.txt 
    from table1');或者做一个循环
    procedure TfrmMain.actTransToTextExecute(Sender: TObject);
    var
      i:integer;
      j:integer; //现有的字段个数,按DBGrid中字段顺序保存DBGrid中可见字段
      SavePlace: TBook;
      fsTxt:TFileStream;
      s,strSeparator:string;
      arrayVisibleFieldID:array [0..255] of integer;
    begin
      if tvGroup.Selected.Text<>'查询结果' then
        dlgSave.FileName :=tvGroup.Selected.Text
      else
        dlgSave.FileName :=tvGroup.Selected.Text+'_'+edtSearch.Text ;
      if not dlgSave.Execute then Exit;
      strSeparator:=',';
      if dlgSave.FilterIndex<>2 then //<>'csv'
        strSeparator:=InputBox('请输入分隔符','',',');
      screen.Cursor:=crHourGlass;
      fsTxt:=TFileStream.Create(dlgSave.FileName,fmCreate);//fmCreate or fmOpenWrite  with DBGrid1.DataSource.DataSet do
      begin
        DisableControls;
        SavePlace := GetBook;
        j:=0;//预设可见Field为0个
        for i:=0 to DBGrid1.Columns.Count-1 do
          if DBGrid1.Columns.Items[i].Visible then
          begin
            arrayVisibleFieldID[j]:=DBGrid1.Columns.Items[i].Field.Index;
            s:=DBGrid1.Columns.Items[i].Title.Caption+strSeparator;
            fsTxt.Write(PChar(s)^, Length(s));
            j:=j+1;
          end;
        fsTxt.Seek(-1,soFromCurrent);
        s:=#13+#10;
        fsTxt.Write(PChar(s)^, Length(s));
        j:=j-1;
        First;
        while not Eof do
        begin
          for i:=0 to j do
          begin
            s:=Fields[arrayVisibleFieldID[i]].AsString+strSeparator;
            fsTxt.Write(PChar(s)^, Length(s));
          end;
          fsTxt.Seek(-1,soFromCurrent);
          s:=#13+#10;
          fsTxt.Write(PChar(s)^, Length(s));
          Next;
        end;
        GotoBook(SavePlace);
        FreeBook(SavePlace);
        EnableControls;
      end;   //with
      screen.Cursor:=crDefault;
      fsTxt.Free;
    end;
    如果还有什么问题可以与我联系
      

  4.   

    非常谢谢各位,特别是dnazj(基因工厂),我用的是ADO DBGrid的数据源是ADODateSet,其中的commandtext的属性是ACCESS数据表 请问如何操作 谢谢!