可以的,我曾經寫過,
但面對復雜的業務邏輯,作用不大的.
實際就是把clientdataset轉換成sql語句的.;思路如下
procedure TMrp.dspProdprocBeforeUpdateRecord(Sender: TObject;
SourceDS: TDataSet;
DeltaDS: TClientDataSet;
UpdateKind: TUpdateKind;
var Applied: Boolean);
...
if uppercase(SourceDs.name)=uppercase('adoQprodproc') then
case updateKind of
ukModify:begin
create_D_update_sql(adoq,deltads,table名,[要操作的字段]);
end;
end;
....
procedure Create_D_update_sql(var sql: Tadoquery;
cds: Tclientdataset;
tabname: string;
strFld: array of string);
var
flds:integer;
tmpsql:string;
begin
if not ChkRecordExist(cds,tabname,strfld) then
//因為是ukModify 檢查記錄是否重復
begin
// setlasterror(ModifyError);
raise exception.Create('Record was chaged by another,Modify failure!');
end;
sql.close;
sql.sql.clear;
tmpsql:=' update '+tabname+' set ';
for flds:=0 to high(strFld)do
begin
tmpsql:=tmpsql+strfld[flds]+'=:'+trim(strfld[flds]+'1');
if flds<>high(strFld) then
tmpsql:=tmpsql+',';
end;
tmpsql:=tmpsql+' where ';
for flds:=0 to high(strFld)do
begin
if not (varisempty(cds.fieldbyname(strfld[flds]).oldvalue) or (cds.fieldbyname(strfld[flds]).oldvalue=null)) then
begin
tmpsql:=tmpsql+strfld[flds]+'=:'+trim(strfld[flds]+'2');
tmpsql:=tmpsql+' AND ';
end;
end;
tmpsql:=tmpsql+'1=1';
sql.SQL.add(tmpsql);
for flds:=0 to high(strFld)do
begin
if varisempty(cds.fieldbyname(strfld[flds]).newvalue) or (cds.fieldbyname(strfld[flds]).newvalue=null) then
sql.Parameters.ParamByName(TRIM(strfld[flds])+'1').value:=cds.fieldbyname(strfld[flds]).oldvalue
else
sql.Parameters.ParamByName(TRIM(strfld[flds])+'1').value:=cds.fieldbyname(strfld[flds]).newvalue;
if not (varisempty(cds.fieldbyname(strfld[flds]).oldvalue) or (cds.fieldbyname(strfld[flds]).oldvalue=null)) then
sql.Parameters.ParamByName(TRIM(strfld[flds])+'2').value:=cds.fieldbyname(strfld[flds]).oldvalue;
end;