通过 SQL.ADD('...') 向数据库中添加记录,可是结果却是 添加了两条同样的记录(主键除外),调试过程中发现,语句也只是执行了一次.
请问这是什么原因造成的 ?

解决方案 »

  1.   

    procedure TfrmPerson.btnOkClick(Sender: TObject);             //完成
    begin
      SaveDate;
      close;
    end;procedure TfrmPerson.SaveDate ;                            //保存数据
    var
      InsertStr: string;
    begin
      InsertStr:= #39 + edtName.Text + #39 + ',' + #39 + cbxSex.Text + #39 + ',' +  cbxAge.Text + ',';
      InsertStr:= InsertStr + cbxYear.Text + '-' + cbxMonth.Text + '-' + cbxDay.Text ;
      With  DataMdl.ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('insert into Family1 values(' + InsertStr + '); '); //保存数据
        SQL.Add('select fname as 姓名,fsex as 性别,fage as 年龄,fbirth as 出生年月 from family1');          //刷新数据
        ExecSql;
        Open;
      end;
      frmMain.dgridPerson.DataSource:= DataMdl.DataSource1 ;
    end;
      

  2.   

    With  DataMdl.ADOQuery1 do
    这样试试
      begin
        Close;
        SQL.Clear;
        SQL.Add('insert into Family1 values(' + InsertStr + '); '); //保存数据
        ExecSql;
        SQL.Clear;
        SQL.Add('select fname as 姓名,fsex as 性别,fage as 年龄,fbirth as 出生年月 from family1');          //刷新数据
        Open;
      end;
      

  3.   

    因为你没有把ADOQuery1.clear
    就又select 所以ADOQuery1就把原来的那个先执行了一遍再select
    明白?
      

  4.   

    execsql执行一遍
    open执行一遍
    一加一等于几?
      

  5.   

    那 ExecSql . Open 有什么区别吗 ?
      

  6.   

    execsql:执行不返回数据集的SQL命令;
    Open:执行返回数据集的SQL命令;
    可以知道为什么为执行两次吧;^_^