服务端代码:
uses
Data.FireDACJSONReflect,
FireDAC.Stan.Storage, FireDAC.Stan.StorageBin, FireDAC.Stan.StorageJSON,
FireDAC.Stan.StorageXML;
1)查询
function TServerMethods1.QuerySql2(const accountNo, sql: string): TFDJSONDataSets;
var
d: TfrmDB;
begin
Result := nil;
if (accountNo = ‘‘) or (sql = ‘‘) then
Exit;
d := GetDBPool(accountNo).Lock;
if not Assigned(d) then
Exit;
try
try
d.qryOpen.Close;
d.qryOpen.sql.Clear;
d.qryOpen.sql.Text := sql;
d.qryOpen.Open;
Result := TFDJSONDataSets.Create;
TFDJSONDataSetsWriter.ListAdd(Result, ‘1‘, d.qryOpen);
except
on e: Exception do
begin
Result := nil;
Log.WriteLog(‘TServerMethods1.QuerySql2 ‘ + e.Message);
end;
end;
finally
d.qryOpen.Close;
GetDBPool(accountNo).Unlock(d);
end;
end;
2)提交
function TServerMethods1.SaveData2(const accountNo, tableName: string; delta: TFDJSONDeltas): Boolean;
var
d: TfrmDB;
LApply: IFDJSONDeltasApplyUpdates;
begin
Result := False;
if (accountNo = ‘‘) or (tableName = ‘‘) or (delta = nil) then
Exit;
d := GetDBPool(accountNo).Lock;
if not Assigned(d) then
Exit;
try
try
d.qryOpen.Close;
d.qryOpen.sql.Clear;
d.qryOpen.sql.Text := ‘select * from ‘ + tableName + ‘ where 1=2‘;
d.qryOpen.Open;
LApply := TFDJSONDeltasApplyUpdates.Create(delta);
LApply.ApplyUpdates(0, d.qryOpen.Command);
Result := LApply.Errors.Count = 0;
except
on e: Exception do
begin
Result := False;
Log.WriteLog(‘TServerMethods1.SaveData2 ‘ + e.Message);
end;
end;
finally
d.qryOpen.Close;
GetDBPool(accountNo).Unlock(d);
end;
end;
客户端代码:
uses
FireDAC.Stan.StorageJSON, FireDAC.Stan.StorageBin
,Data.FireDACJSONReflect, FireDAC.Stan.StorageXML ,FireDAC.Stan.Storage;
切记:
FDMemTable1.CachedUpdates := True;
1)查询
procedure TForm1.btnQueryClick(Sender: TObject);
var
r: TServerMethods1Client;
LDataSetList: TFDJSONDataSets;
LDataSet: TFDDataSet;
begin
r := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
LDataSetList := r.QuerySql2(‘0‘, ‘select * from t1‘);
LDataSet := TFDJSONDataSetsReader.GetListValueByName(LDataSetList,‘1‘);
FDMemTable1.Close;
FDMemTable1.AppendData(LDataSet);
r.Free;
end;
2)提交
procedure TForm1.btnSaveClick(Sender: TObject);
var
r: TServerMethods1Client;
d: TFDJSONDeltas;
begin
r := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
if FDMemTable1.State in dsEditModes then
FDMemTable1.Post;
if FDMemTable1.ChangeCount = 0 then
Exit;
d := TFDJSONDeltas.Create;
TFDJSONDeltasWriter.ListAdd(d, ‘1‘, FDMemTable1);
if r.SaveData2(‘0‘, ‘t1‘, d) then
Self.Caption := ‘save ok‘
else self.Caption := ‘save fail‘;
r.Free;
end;