Delphi XE and ServerMethods
With Delphi XE I create a client-server application with "DataSnap Server" Wizard.
In the ServerMethodUnit
I Define a TSQLQuery
with sql property like this "Select * from TABLE"
I define a function called ChangeSQL
in ServerMethodUnit
too, what change the sql property by example SELECT * from TABLE where ID = 5
When i Call ChangeSQL
form Client Application through TSQLServerMethod
, the ChangeSQL
function
change SQL
property in the ServerMethodUnit
, but when it leaves the function ChangeSQL
, the TSQLQuery
not containing the new sql command but has the original sql.
EDIT: Addition of code sample
I create a new a client-server application with "DataSnap Server" Wizard.
In ServerMethodsUnit
:
I put a TSQLConnection
to my database.
I Put a TSQLQuery
call MyQuery
, with SQL
property = 'Select * from client'
I put a TDataSetProvider
to TSQLQuery
And define a function like this:
function TServerMethods1.ClientFiltrer (ID:integer):integer;
begin
// ------------------------------------------------ // At this point, MyQuery.RecordCount = all records (both the first time and the following) // ------------------------------------------------
if MyQuery.Active 开发者_如何学运维then
MyQuery.close;
MyQuery.SQL.Clear;
MyQuery.SQL.Add(' SELECT * from CLIENT where ID = '+StrToInt (ID));
MyQuery.Open;
result := MyQuery.RecordCount;
// ------------------------------------------------
// At this point, MyQuery.RecordCount = ONE record
// ------------------------------------------------
end;
AND THEN I create a new application with "DataSnap CLient"
In the ClientModuleUni
:
I put a ClientDataSet
, called MyClientDS
with RemoteServer
and ProviderName
to my MyQuery
provider.
I put a TSQLServerMethod
called MyMethod
pointing to ClientFilter
in ServerMethodsUnit
In a form of the client application
Then I put A DataSource
and a DBGrid
to MyClientDs
and open it.
I put a Tbutton
with code OnClick
:
begin
MyClientDs .Close;
MyMethod .ParamByName('ID').Value := 5;
MyMethod .ExecuteMethod;
showmessage (MyMethod .ParamByName('ID').AsString);
// AT THIS POINT, returned value is ONE.
// I supposed that this Query had been filtered on
// the server and the client then show the filtered
// data, but it always shows the original query
MyClientDs .Close;
end;
In the DBGrid
The I see all records of Client Table, ALWAYS.
I try filter ClientTable
clicking in the button, but in the DBGrid
always appears all the record.
The problem is here:
MyQuery.SQL.Clear;
MyQuery.SQL.Add(' SELECT * from CLIENT where ID = '+StrToInt (ID));
MyQuery.Open;
result := MyQuery.RecordCount;
// ------------------------------------------------
// At this point, MyQuery.RecordCount = ONE record
// ------------------------------------------------
When you assign ID StrToInt(ID)
, you are not creating a parameter. Therefore, later when you do the `ParamByName' to change it, there's no parameter there to change.
Change the lines above to
MyQuery.SQL.Text := `SELECT * FROM client WHERE ID = :ID`;
MyQuery.ParamByName('ID').AsInteger := ID;
MyQuery.Open;
This should fix the problem.
the answer to this in DSServerClass1.LifeCycle.
It was "invocation". I've changed to "session".
There is a continuous disconnect to the server with "invocation" and then restores the defaults.
Anyone knows how to work with "invocation"?
精彩评论