如何判断kbmMWClientQuery当前记录的增改状态?
有朋友问我,客户端使用了kbmMWClientQuery,对其进行了编辑后,对于指定的记录,如何判断是否是增加的记录,或者是被修改后的记录?
下面这个函数,返回aDataSet当前记录的修改状态:
function TForm5.GetUpdateStatus(aDataSet: TkbmMWCustomClientQuery): TUpdateStatus; var i: TkbmNativeInt; pRec, pOrigRec, pOldRec: PkbmRecord; st: TUpdateStatus; aOldV, aNewV: Variant; IsFieldValueChanged:Boolean; begin pRec := pkbmRecord(aDataSet.ActiveBuffer); if pRec = nil then Exit; pOrigRec:=pRec; while pOrigRec^.PrevRecordVersion <> nil do begin pOrigRec := pOrigRec^.PrevRecordVersion; end; if pRec^.UpdateStatus = usDeleted then begin if pOrigRec^.UpdateStatus = usInserted then st := usUnmodified; //增加的记录又删除记为usUnmodified. end else if pOrigRec^.UpdateStatus = usInserted then st := usInserted else st := pRec^.UpdateStatus; if st = usModified then with aDataSet do begin IsFieldValueChanged:=False; pOldRec := OverrideActiveRecordBuffer; try for i := 0 to FieldCount - 1 do begin OverrideActiveRecordBuffer := pRec; // 指向修改后的数据 aNewV := FieldByName(Fields[i].FieldName).Value; OverrideActiveRecordBuffer := pOrigRec; // 指向修改前的数据 aOldV := FieldByName(Fields[i].FieldName).Value; if aNewV <> aOldV then begin IsFieldValueChanged:=True; Break; end; end; if not IsFieldValueChanged then st:= usUnmodified; finally OverrideActiveRecordBuffer := pOldRec; end; end; result:=st; end;
这是测试代码:
procedure TForm5.Button3Click(Sender: TObject); var st:TUpdateStatus; begin st:= GetUpdateStatus(m); if st = TUpdateStatus.usModified then Memo1.Lines.Add(' usModified ' + ' RecordID=' + m.RecordID.ToString) else if st = TUpdateStatus.usInserted then Memo1.Lines.Add(' usInserted' + ' RecordID=' + m.RecordID.ToString) else if st = TUpdateStatus.usDeleted then Memo1.Lines.Add(' usDeleted' + ' RecordID=' + m.RecordID.ToString) else if st = TUpdateStatus.usUnmodified then Memo1.Lines.Add(' usUnmodified' + ' RecordID=' + m.RecordID.ToString); end;
参考: https://www.cnblogs.com/kinglandsoft/p/18091004
注意:只适用于kbmMWClientQuery,不适用于kbmMemTable。