上传客户端的CLIENTDATASET.delta到服务器的clientdataset.data,服务端解析clientdataset的数据生成相应的SQL语句。 相对于直接调用datasetprovider.applyupdates()方法提交数据而言,前者的可控性更强,对于某些要求灵活性很强的场合,前者可能是必须的提交方式。 procedure TBaseService.ApplyUpdates(const Delta: OleVariant; TableName, KeyField: WideString);
function vartosql(value: Variant): wideString; procedure TBaseService.InnerApplyUpdates(TableName, KeyField: WideString); if (cdsDelta.Fields[i-1].NewValue <> System.Variants.Unassigned and (cdsDelta.Fields[i - 1].Tag = 1) then
begin if s1 = '' then s1 := Trim(cdsDelta.Fields[i - 1].FieldName) + ' = ' + VarToSql(cdsDelta.Fields[i - 1].Value) else s1 := s1 + ',' + Trim(cdsDelta.Fields[i - 1].FieldName) + ' = ' + VarToSql(cdsDelta.Fields[i - 1].Value); end; if s1 <> '' then begin CmdStr := 'Update ' + TableName + ' Set ' + s1 + ' Where ' + KeyField + ' = ' + s2; end; end; usInserted: begin s1 := ''; s2 := ''; for i := 1 to cdsDelta.FieldCount do if (not cdsDelta.Fields[i - 1].IsNull) and (cdsDelta.Fields[i - 1].Tag = 1) then begin if s1 = '' then begin s1 := Trim(cdsDelta.Fields[i - 1].FieldName); s2 := VarToSql(cdsDelta.Fields[i - 1].Value); end else begin s1 := s1 + ',' + Trim(cdsDelta.Fields[i - 1].FieldName); s2 := s2 + ',' + VarToSql(cdsDelta.Fields[i - 1].Value); end; end; if s1 <> '' then begin CmdStr := 'Insert into ' + TableName + '(' + s1 + ') Values (' + s2 + ')'; end; end; usDeleted: begin s2 := VarToSql(cdsDelta[KeyField]); CmdStr := 'Delete ' + TableName + ' Where ' + KeyField + ' = ' + s2; end; end; if CmdStr <> '' then Cmd.Execute(CmdStr); cdsDelta.Next; end; cdsDelta.First; cdsDelta.EmptyDataSet; cdsDelta.Close; end; end; end; *************************
在更新时, 如果新值为Null, 会被漏掉, 解决办法如下:
把usModified中的not Field.IsNull改为Field.NewValue <> System.Variants.Unassigned |
|