原來使用ado來訪問數據庫,用在DataSnap中也很方便。後來便一直使用UniDac,可發現UniDac如果用在DataSnap中要比ado麻煩很多,尤其對自增長字段、缺省值的處理上,感覺對DataSnap支持不好(普通C/S應用還是非常好的)。
在Unidac官網上看到一個關於處理DataSnap中的AutoInc字段問題,記錄在下面,我沒有驗證。
I use DataSnap delphi 2010.
UniQuery Component DMLRefresh Can fetch autoinc value,
but I cannot use it with datasnap.
I use Follow Code.
procedure TForm.dspDataAfterUpdateRecord(Sender: TObject; SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind); begin if UpdateKind = ukInsert then quData.Fields[0].NewValue := GetLastID; end;
GetLastID function is fetch autoinc value.
the code works grate with DBExpress. But not UniDac.
Answer:
To solve this problem, it's needed to set the following options:
Code: Select all
TDataSetProvider.ResolveToDataSet := True;
TUniQuery.RequiredFields := False;
TUniQuery.SetFieldsReadOnly := False;In this case UniDAC will generate the SQL code, and the identity field value will be set automatically to the TUniQuery component. To update the identity field value in the TClientDataSet, component it's needed to use the following code:
Code: Select all
procedure TForm1.DataSetProvider1AfterUpdateRecord(Sender: TObject; SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);
begin
if UpdateKind = ukInsert then
DeltaDS.Fields[0].NewValue := UniQuery1.Fields[0].AsInteger;
end;