码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-05-06 15:07:51      阅读:1028      评论:0      收藏:0      [点我收藏+]

标签:app   etl   res   转换   ase   except   edit   ada   line   

//读取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多次更新时的错误

标签:app   etl   res   转换   ase   except   edit   ada   line   

原文地址:http://www.cnblogs.com/zsfishman/p/6816728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!