delphi把Clientdataset的Delta保存到数据库

本文介绍了一个使用Delphi实现的通用函数UpdateTableData,该函数用于更新指定表中的记录。函数接收表名、主键字段及待更新的数据作为参数,并通过创建数据集和提供者组件来完成更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. //ATableName-表名, AKeyField-主键,多个主键用;隔开,如 ;pid;times;  from:unit HlsImplBase;  
  1. function THlsImplBase.UpdateTableData(ATableName, AKeyField: AnsiString; var AData: Variant): Boolean;  
  2. var  
  3.   LSql: string;  
  4.   LDataSet: TDataSet;  
  5.   LProvider:  TProvider;  
  6.   LErrCount: Integer;  
  7.   I: Integer;  
  8. begin  
  9.   Result := False;  
  10.   if ATableName = '' then Exit;  
  11.   if AKeyField = '' then Exit;  
  12.   
  13.   LSql := 'select * from ' + ATableName + ' where 1 = 2';  
  14.   LDataSet := CreateDataSet(LSql);  
  15.   if not Assigned(LDataSet) then Exit;  
  16.   
  17.   LProvider := TProvider.Create(nil);  
  18.   LProvider.UpdateMode := upWhereKeyOnly;  
  19.   LProvider.DataSet := LDataSet;  
  20.   AKeyField := LowerCase(AKeyField);  
  21.   for I := 0 to LDataSet.FieldCount - 1 do  
  22.   begin  
  23.     if (LowerCase(LDataSet.Fields[I].FieldName) = AKeyField) or  
  24.         (Pos(';' + LowerCase(LDataSet.Fields[I].FieldName) + ';', AKeyField) > 0then  
  25.       LDataSet.Fields[I].ProviderFlags := LDataSet.Fields[I].ProviderFlags + [pfInKey]  
  26.     else  
  27.       LDataSet.Fields[I].ProviderFlags := [];  
  28.   end;  
  29.   try  
  30.     try  
  31.       LProvider.ApplyUpdates(AData, -1, LErrCount);  
  32.     except  
  33.     end;  
  34.     Result := LErrCount = 0;  
  35.   finally  
  36.     LDataSet.Free;  
  37.     LProvider.Free;  
  38.   end;  
  39.     
  40. end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值