[英]How to insert records with DataSnap
在许多教程中,我阅读了如何在datasnap客户端中从数据库中选择数据,例如pe来完成dbgrid。 但是我现在需要知道如何插入或更新一行,例如“新客户端”。 大家可以推荐我一本书或教程吗?
除了clientclassesunit之外,我在客户端的clientdatamodule上有一个sqlconnection。 我向我证明了带有插入SQL语句的SQLQuery,但是它不起作用。
在另一方面,我在服务器端:
procedure TServerMethods1.nuevocheque(idcliente,numero,cuenta,idbanco : integer; fr,fc, titular:string ;importe:Double;cobrado:Boolean);
var
ucheque:integer;
begin
with qicheque do
begin
Open;
ParamByName('idcliente').AsInteger:=idcliente;
ParamByName('numero').AsInteger:=numero;
ParamByName('fr').AsDate:=StrToDate(fr);
ParamByName('fc').AsDate:=StrToDate(fc);
ParamByName('importe').AsFloat:=importe;
ParamByName('titular').AsString:=titular;
ParamByName('cobrado').AsBoolean:=cobrado;
ParamByName('cuenta').AsInteger:=cuenta;
ExecSQL();
end;
end;
通过这种方法,我尝试插入该语句,该语句位于组件的SQL属性中。
在客户端,我有一个TSQLServerMethod,它称为“ nuevocheque”:
procedure TForm4.BGuardarClick(Sender: TObject);
var
idcliente,numero,cuenta,idbanco:integer;
titular:string;
cobrado:Boolean;
fr,fc:string;
importe:Double;
begin
ClientModule1.nuevocheque.Create(nil);
with ClientModule1.nuevocheque do
begin
idcliente:=1;
numero:=StrToInt(ENumero.Text);
cuenta:=StrToInt(Ecuenta.Text);
idbanco:=1;
titular:=ENombre.Text;
cobrado:=False;
importe:=StrToFloat(EMonto.Text);
fr:=EFechaEmision.Text;
fc:=EFechacobro.Text;
end;
end;
但是它不起作用。 谢谢您帮忙
好吧,我实现了将数据插入到我已经设计好的mysql数据库中。
这是delphi中的te代码成一个按钮:
procedure TForm4.BGuardarClick(Sender: TObject);
var
idcliente,numero,cuenta,idbanco:integer;
titular:string;
cobrado:Boolean;
fr,fc:string;
importe:Double;
a:TServerMethods1Client;
interes:Double;
begin
a:=TServerMethods1Client.Create(ClientModule1.SQLConnection1.DBXConnection);
begin
idcliente:=Unit3.id;
numero:=StrToInt(ENumero.Text);
cuenta:=StrToInt(Ecuenta.Text);
idbanco:=lcbbanco.KeyValue;
titular:=ENombre.Text;
cobrado:=False;
if (EP.Text<>'') then
begin
importe:=StrToFloat(EHC.Text);
end
else
begin
importe:=StrToFloat(EMonto.Text);
end;
fr:=EFechaEmision.Text;
fc:=EFechacobro.Text;
end;
a.nuevocheque(idcliente,numero,cuenta, idbanco,fr,fc,titular,importe,cobrado);
end;
我已经使用M Diwo这样的SQL组件调用了create()方法。
我太开心了。 谢谢大家
我不知道您用什么作为数据库连接,为方便起见,我对dbGO(由variant传递的参数)做了一些修改。
我还从服务器方法中获得了一个功能,像这样,可以通知客户端出现了问题(查询,连接等)。 这是服务器方法:
//server
function TServerMethods1.NuevoCheque(idcliente, numero, cuenta,
idbanco: integer; fr, fc, titular: string; importe: Double;
cobrado: Boolean): Boolean;
begin
try
with qicheque, Parameters do
begin
Close;
ParamByName('idcliente').Value:=idcliente;
ParamByName('numero').Value:=numero;
ParamByName('fr').Value:=StrToDate(fr);
ParamByName('fc').Value:=StrToDate(fc);
ParamByName('importe').Value:=importe;
ParamByName('titular').Value:=titular;
ParamByName('cobrado').Value:=cobrado;
ParamByName('cuenta').Value:=cuenta;
ExecSQL();
end;
Result := true;
except
Result := false;
//raise; <-- uncomment if you want to handle this properly in your code
end;
end;
我想为客户端生成了一个代理单元,该代理单元通常创建一个称为ServerMethods1的对象?
您必须将客户端dbx连接传递给它-之所以这样说是因为我看到您在代码中添加了nil。
// client
procedure TfrmClient.BGuardaClick(Sender: TObject);
var
sm : TServerMethods1Client; // <-- generated by proxy generator
idcliente,numero,cuenta,idbanco : integer;
fr,fc, titular : string ;
importe : Double;
cobrado : Boolean;
begin
sm := TServerMethods1Client.Create(SQL.DBXConnection);
if sm.nuevocheque(idcliente,numero,cuenta,idbanco, fr,fc, titular, importe, cobrado) then
// ok
else
// error
sm.Free;
end;
心连心
您可以使用对远程方法的调用,但是它们不会自动更新您的数据感知控件。 Datasnap可以处理它。 首先,您需要在客户端上添加/更新/删除数据。 即使在您“发布”时,它也会在TClientDataset管理的本地缓存中发生。 准备好后,您需要调用Apply()方法将更改“应用”到远程服务器。 调用它时,服务器上的提供程序组件会从客户端数据集中收到带有要更改记录的“增量”,并将自动生成所需的INSERT / UPDATED / DELETE SQL语句。 如果您不喜欢它们,或者需要执行更复杂的处理,则可以使用提供者事件自己为每个更改的记录执行所需的操作,然后告诉提供者您执行了此操作以避免自动处理。 然后,提供程序将“增量”传递回客户端,在客户端用于更新数据感知控件。 您也可以在传递回“ delta”之前对其进行修改。 阅读文档中有关Datasnap体系结构的说明-这是一个多步设计,其中多个组件可以工作以实现多层实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.