码迷,mamicode.com
首页 > 其他好文 > 详细

通用的更新方法

时间:2016-02-28 22:46:03      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

function THlsImplBase.UpdateTableData(ATableName, AKeyField: AnsiString; var AData: Variant): Boolean;
var
  LSql: string;
  I: Integer;
  
  {$IFDEF MSSQL}
  LProvider:  TProvider;
  LDataSet: TDataSet;
  {$ENDIF}

  {$IFDEF ORACLE}
  LOraQuery:      TOraQuery;
  LDataProvider:  TOraProvider;
  {$ENDIF}
  LErrCount: Integer;
begin
  Result := False;
  if ATableName = ‘‘ then Exit;
  
  if AData = null then
  begin
    Result := True;
    Exit;
  end;  
  
  {$IFDEF MSSQL}
  if AKeyField = ‘‘ then Exit;

  LSql := select * from  + ATableName +  where 1 = 2;
  LDataSet := CreateDataSet(LSql);
  if not Assigned(LDataSet) then Exit;

  LProvider := TProvider.Create(nil);
  LProvider.UpdateMode := upWhereKeyOnly;
  LProvider.DataSet := LDataSet;
  AKeyField := LowerCase(AKeyField);
  for I := 0 to LDataSet.FieldCount - 1 do
  begin
    if (LowerCase(LDataSet.Fields[I].FieldName) = AKeyField) or
        (Pos(; + LowerCase(LDataSet.Fields[I].FieldName) + ;, AKeyField) > 0) then
      LDataSet.Fields[I].ProviderFlags := LDataSet.Fields[I].ProviderFlags + [pfInKey]
    else
      LDataSet.Fields[I].ProviderFlags := [];
  end;
  try
    try
      LProvider.ApplyUpdates(AData, -1, LErrCount);
    except
    end;
    Result := LErrCount = 0;
  finally
    LDataSet.Free;
    LProvider.Free;
  end;
  {$ENDIF}

  {$IFDEF ORACLE}
  LSql := select * from  + ATableName +  where 1=2;
  LSql  := StringReplace(LSql, (nolock), ‘‘, [rfReplaceAll, rfIgnoreCase]);
  LOraQuery           := TOraQuery.Create(nil);
  LOraQuery.Session   := FConnection;
  LOraQuery.SQL.Text  := LSql;
  LOraQuery.Open();

  LDataProvider         := TOraProvider.Create(nil);
  LDataProvider.DataSet := LOraQuery;
  LDataProvider.UpdateMode  := upWhereKeyOnly;

  AKeyField := LowerCase(AKeyField);
  for I := 0 to LOraQuery.FieldCount - 1 do
  begin
    if (LowerCase(LOraQuery.Fields[I].FieldName) = AKeyField) or
        (Pos(; + LowerCase(LOraQuery.Fields[I].FieldName) + ;, AKeyField) > 0) then
      LOraQuery.Fields[I].ProviderFlags := LOraQuery.Fields[I].ProviderFlags + [pfInKey]
    else
      LOraQuery.Fields[I].ProviderFlags := [];
  end;

  try
    try
      LDataProvider.ApplyUpdates(AData, -1, LErrCount);
    except
    end;
    Result := LErrCount = 0;
  finally
    LDataProvider.Free;
    LOraQuery.Close;
    LOraQuery.Free;
  end;
  {$ENDIF}

end;
//Provider 用来更新数据

 

function THisIntfService.UpdateTable(ADatas: TUpdateTableDatas): Boolean;
var
  I:      Integer;
  LData:  Variant;
  LTableData: TUpdateTableData;
begin
  Result      := False;
  if Length(ADatas) = 0 then Exit;
  
  StartTrans();

  try
    for I := Low(ADatas) to High(ADatas) do
    begin
      LTableData  := ADatas[I];
      LData       := LTableData.DataValue;
      HisMzFuncs.UnCompressVariants(LData);
      if not Self.UpdateTableData(LTableData.TableName, LTableData.KeyFields, LData) then//里面利用了Provider 进行提交数据
      begin
        Self.RollBack;
        Exit;
      end;
    end;

  except
    RollBack();
    Exit;
  end;

  Commit();
  Result  := True;

end;

 

通用的更新方法

标签:

原文地址:http://www.cnblogs.com/Morlin-julier/p/5225699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!