//读取mysql保存到fdMemTable中
procedure TForm3.btnOpen1Click(Sender: TObject);
var
stream, stream2: TMemoryStream;
buf: TBytes;
begin
stream := TMemoryStream.Create;
try
try
FDQuery1.Close;
FDQuery1.SQL.Clear;
FDQuery1.Open(‘select * from GLFMKHJL‘);
FDQuery1.SaveToStream(stream, TFDStorageFormat.sfBinary);
// 流转换为BUFFER,将BUFFER通过网络分包传输
stream.Position := 0;
SetLength(buf, stream.Size);
stream.Read(buf[0], stream.Size);
// BUFFER转换为流
stream2 := TMemoryStream.Create;
stream2.Write(buf[0], Length(buf));
stream2.Position := 0;
FDMemTable1.CachedUpdates := True;
FDMemTable1.Close;
FDMemTable1.LoadFromStream(stream2, TFDStorageFormat.sfBinary);
except
on E: Exception do
begin
showmessage(‘错误:‘ + E.Message);
end;
end;
finally
FDQuery1.Close;
stream.Free;
stream2.Free;
end;
end;
//从fdMemTable中编辑数据,通过fdQuery保存到mysql
procedure TForm3.btnSaveClick(Sender: TObject);
var
stream, stream2: TMemoryStream;
buf: TBytes;
begin
stream := TMemoryStream.Create;
try
try
if FDMemTable1.State in [dsInsert, dsEdit] then
FDMemTable1.Post;
if FDMemTable1.ChangeCount = 0 then
Exit;
FDMemTable1.ResourceOptions.StoreItems := [siDelta, siMeta]; // 只提交修改的数据
FDMemTable1.SaveToStream(stream, TFDStorageFormat.sfBinary);
// 流转换为BUFFER,将BUFFER通过网络分包传输
stream.Position := 0;
SetLength(buf, stream.Size);
stream.Read(buf[0], stream.Size);
// BUFFER转换为流
stream2 := TMemoryStream.Create;
stream2.Write(buf[0], Length(buf));
stream2.Position := 0;
FDQuery1.Close;
FDSchemaAdapter1.Close;
FDQuery1.SchemaAdapter := FDSchemaAdapter1;
FDQuery1.CachedUpdates := True;
FDQuery1.SQL.Clear;
FDQuery1.sql.Text := ‘select * from GLFMKHJL where 1=2‘;
FDQuery1.Open;
FDSchemaAdapter1.LoadFromStream(stream2, TFDStorageFormat.sfBinary);
ShowMessage( FDSchemaAdapter1.ApplyUpdates.ToString );//返回0成功
except
on E: Exception do
begin
showmessage(‘错误:‘ + E.Message);
end;
end;
finally
FDQuery1.Close;
stream.Free;
stream2.Free;
end;
end;