全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1043
推到 Plurk!
推到 Facebook!

BeforeUpdateRecord時DeltaDS問題

尚未結案
alice
初階會員


發表:41
回覆:49
積分:28
註冊:2002-04-30

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-01-06 15:43:46 IP:211.75.xxx.xxx 未訂閱
原想若資料有異動時, SQLClientDataSet.Delta.Fields會有值, 若無做異動修改, 則會是NULL, 所以根據這想法 撰寫如下的來動態產生Update 的SQL COMMAND,  判斷Field.isNull表未異動,就略過, 現在發現若異動後的是null, 就會被我略過, 沒有更新到,  有沒有什麼方式, 該我知道這Field是我異動成null, 而非未曾異動的null?? 或有其他方式可以關連原SQLClientDataSet知道修改後的值 或有其他解決方法??     
procedure SQLClientDataSetBeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet;
  UpdateKind: TUpdateKind; var Applied: Boolean);
var SQLSTR,Wheres,FieldSTR,TableName:string;
    i:integer;qUpQuery:TSQLQuery;
begin
  Applied:=false;
  qUpQuery:=TSQLQuery.Create(Self);  
  qUpQuery.SQLConnection:=SQLConnection;
  qUpQuery.SQL.Clear;
  TableName:='TEST';
  if UpdateKind= ukModify then begin
     SQLSTR:='';
     FOR i := 0 TO DeltaDS.FieldCount -1 do begin
       FieldSTR:=DeltaDS.Fields.Fields[i].FieldName;  
       if pfInUpdate in SQLClientDataSet.Fields.FindField(FieldSTR).ProviderFlags then begin
          if ( not DeltaDS.Fields.Fields[i].IsNull) then begin
             if SQLSTR='' then
                SQLSTR:=' update '   TableName  ' set ' FieldSTR '=:' FieldSTR
             else
                SQLSTR:= SQLSTR ',' FieldSTR '=:' FieldSTR;
          end;
       end;
     //產生where 條件
       if ( pfInWhere in SQLClientDataSet.Fields.FindField(FieldSTR).ProviderFlags ) then begin
           if VarIsNull(DeltaDS.Fields.FieldByName(FieldSTR).OldValue) then
              if wheres='' then wheres:=' WHERE ' FieldSTR ' IS NULL ' else
                 wheres:=wheres ' AND ' FieldSTR ' IS NULL '
           else
             if wheres='' then wheres:=' WHERE ' FieldSTR '=:OLD_' FieldSTR else
                wheres:=wheres ' AND ' FieldSTR '=:OLD_' FieldSTR;
        end;
     end;
  end;      qUpQuery.SQL.Add(SQLSTR wheres);      //設定params開始
  FOR i := 0 TO DeltaDS.FieldCount -1 do begin
    FieldSTR:=DeltaDS.Fields.Fields[i].FieldName;
    if UpdateKind in [ukModify] then begin
      if ( not DeltaDS.Fields.Fields[i].IsNull) and
          ( pfInUpdate in SQLClientDataSet.Fields.FindField(FieldSTR).ProviderFlags ) then begin
          qUpdateQuery.Params.ParamByName(FieldSTR).DataType:=DeltaDS.Fields.FieldByName(FieldSTR).DataType;
          qUpdateQuery.Params.ParamByName(FieldSTR).ParamType:=ptInput;
          qUpdateQuery.Params.ParamByName(FieldSTR).Value:=DeltaDS.FieldByName(FieldSTR).Value;
       end;
    end;
    if UpdateKind in [ukModify] then begin
       if ( pfInWhere in SQLClientDataSet.Fields.FindField(FieldSTR).ProviderFlags ) then begin
           if VarIsNull(DeltaDS.Fields.FieldByName(FieldSTR).OldValue) then
          else begin
            OldFieldSTR:='OLD_' FieldSTR;
            qUpdateQuery.Params.ParamByName(OldFieldSTR).DataType:=DeltaDS.Fields.FieldByName(FieldSTR).DataType;;
            qUpdateQuery.Params.ParamByName(OldFieldSTR).ParamType:=ptInput;
            qUpdateQuery.Params.ParamByName(OldFieldSTR).Value:=DeltaDS.Fields.FieldByName(FieldSTR).OldValue;
          end;
       end;
    end;    end;        
發表人 -
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-01-07 12:24:38 IP:147.8.xxx.xxx 未訂閱
You should check against Unassigned, not Null.
speedup
資深會員


發表:19
回覆:259
積分:280
註冊:2003-07-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-01-08 14:24:13 IP:61.224.xxx.xxx 未訂閱
procedure SQLClientDataSetBeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet;
  UpdateKind: TUpdateKind; var Applied: Boolean);
在BeforeUpdateRecord事件中不是有SourceDS,sourceDS代表原始資料,你用SourceDS與DeltaDS兩邊對比不就知道了 混心雜欲 棄修身~唉
------
唉~
系統時間:2024-06-29 13:30:27
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!