使用fdmemTable来代替clientDataset,解决MySQL5.6(含)以上版本用cds多次更新时的错误

Posted 舟山渔夫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用fdmemTable来代替clientDataset,解决MySQL5.6(含)以上版本用cds多次更新时的错误相关的知识,希望对你有一定的参考价值。

//读取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;








































































以上是关于使用fdmemTable来代替clientDataset,解决MySQL5.6(含)以上版本用cds多次更新时的错误的主要内容,如果未能解决你的问题,请参考以下文章

dac FDMemTable

mormot中间件成功匹配客户端FDMemTable和ClientDataSet

FireDAC 下FDMEMTable的的字段自动获取

FireDAC 下FDMEMTable的的字段自动获取

使用c#中的流将文件从服务器传输到客户端时数据丢失

i2c_set_clientdata函数