How to insert records with DataSnap

2.3k views Asked by At

In many tutorials i read how to select data from a database in a datasnap client, p.e. to complete a dbgrid. But i need now to know how to insert or update a row, p.e "new client". Can everybody recommends me a book or tutorial?

I have an sqlconnection on a clientdatamodule on the clientside apart from clientclassesunit. I was prooving wuth an SQLQuery with an insert SQL Statement but it doen't function.

On the other han i have on the server side:

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;

With this method i try to insert, the statement is into SQL property of the component.

On the client side, i have a TSQLServerMethod wich calls "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;

But it doesn´t function. Thank for your help

3

There are 3 answers

4
Fou de la Dame On

I don't know what you use as database connection, for my own convenience I have slightly modified for dbGO (parameters passed by variant).
Also I have made a function from the server method, like this the client can be notified that there has been a problem (with the query, connection,...). Here is the server method:

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


For the client I suppose you generated a proxy unit that generally creates an object called ServerMethods1 ?
You must pass the client dbx connection to this - I say this because I saw you put nil in your code.

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


hth

5
LDS On

You can use calls to remote methods, but they won't automatically update your data aware controls automatically. Datasnap is able to handle it. First, you need to add/update/remove data on the client. It happens in the local cache managed by the TClientDataset, even when you "Post". When you're ready, you need to "apply" changes to the remote server calling the Apply() method. When you call it, the provider component on the server receives a "delta" with the record to change from the client dataset, and will automatically generate the needed INSERT/UPDATED/DELETE SQL statements. If you don't like them, or you need to perform more complex processing, you can use the provider events to perform the needed operations yourself for each changed record and then tell the provider you did it to avoid the automatic processing. Then the provider passes back the "delta" to the client, where it is used to updated the data aware controls. You can also modify the "delta" before it is passed back. Read in the documentation the explanation of the Datasnap architecture - it's a multistep design where several components work to allow for a multi-tier implementation.

0
Caruso On

Well, i achieve inserting data into mysql database i had desgined.

This is te code in delphi into a button:

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;

I've called to method create() with the SQL component such as M Diwo said me.

Im too hapy. Thanks to all