delphi:DBGridEh删除数据后重新查询,数据没有变化。

情况说明:

DBGridEh的datasouce是ds1(tdatasouce),dataset是qry1(TDaoQuery), con1是TADOConnection。

最初用qry1查询数据:

qry1.Connection := con1;
qry1.Close;
qry1.SQL.Clear;
sql := 'select * from studentinfo where 1=1' ;
qry1.SQL.Add(sql);
qry1.Open;

但是有另外一段代码执行了数据删除,没有通过qry1。

qTmp := dbacess.CreateADOQuery; //--- 创建了新的数据库链接
qTmp.Close;
qTmp.SQL.Clear;
sql := 'delete from studentinfo where id=' + edt1.Text;
qTmp.SQL.Add(sql);
qTmp.ExecSQL;
FreeAndNil(qTmp);

此时重新刷新上面的代码,表格数据没有变化。


原因:dataset数据存在缓存,删除操作是在第三方完成后,这就造成了被删除数据还在的情况。

解决办法:

用qry1执行删除语句,再一次删除对应的记录,然后再刷新。

qry1.Connection := con1;
qry1.Close;
qry1.SQL.Clear;

sql := 'delete from studentinfo where id=' + edt1.Text;

qry1.SQL.Add(sql);

qry1.ExecSQL;

qry1.SQL.Clear;
sql := 'select * from studentinfo where 1=1' ;
qry1.SQL.Add(sql);
qry1.Open;

posted @ 2022-06-24 10:59  huiy_小溪  阅读(167)  评论(0编辑  收藏  举报