使用DataSetProvider的ApplyUpdate增加或者修改数据的样例

来源:互联网 发布:黑客特效js 编辑:程序博客网 时间:2024/05/29 18:05

 function TRDMEnterCaseServer.UpdateBugInfo(BugDelta: OleVariant;
  var aMsg: OleVariant): OleVariant;
var
  ErrorCount: Integer;
  BUGID: Integer;
begin
  ErrorCount := 0; // 用于在更新过程中记录发生错误的次数
  Result := False;   // 返回本次操作是否成功
  aMsg := '';   // 若发生错误返回错误信息
  try
    if not VarIsEmpty(BugDelta) then
    begin
      ClientDs.Data := BugDelta;
      ClientDs.First();
      BUGID := 1;
      while not ClientDs.Eof do
      begin
        case ClientDs.UpdateStatus of
          usInserted:
          begin  
            while IDIsExist(adoQryTemp, 'BL_TBL_BUG', 'BUGID', IntToStr(BUGID)) do// 确保BUGID的编号唯一
              BUGID := BUGID + 1;
            ClientDs.Edit();
            ClientDs.FieldByName('BUGID').AsInteger := BUGID;
            ClientDs.Post;
            BUGID := BUGID + 1;
          end;
          usUnmodified:
          begin
            ClientDs.Next();
          end;
        end;
        ClientDs.Next();
      end;
      dsProvider.DataSet := adoQry;
      adoQry.Close;
      adoQry.Connection := DMCaseServer.adoCon1;
      adoQry.SQL.Clear;
      // 若要实现无状态,这一步是必须的,因为要根据以上语句生成更新语句
      adoQry.SQL.Add('Select * from BL_V_BUG where BUGID = -1');
      adoQry.Open;
      dsProvider.ApplyUpdates(ClientDs.Data, -1, ErrorCount);
    end;

    if ErrorCount <> 0 then
    begin
      aMsg := FErrorMsg;
      exit;
    end;
    Result := True;
  except
    on E: Exception do
    begin
      aMsg := E.Message;
    end;
  end;         
end;

 

// 判断某个表中的某个字段中是否存在此值

function IDIsExist(ado: TADOQuery; TableName, FieldName, FieldValue: string): Boolean;
begin
  Result := False;
  with ado do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from ' + TableName + ' where '+ FieldName + ' = ' + FieldValue);
    Open;
    if RecordCount > 0 then
      Result := True;
  end;
end;

原创粉丝点击