I am using Indy Http server component and need to create a new connection and query on the onCommandGet so the thread is using its own connection & query.
What is the code to do this please?
(simple example would be good)
Cheers
SteveW
Connection at runtime
-
AndreyZ
Re: Connection at runtime
Hello,
Here is a code example:
Here is a code example:
Code: Select all
procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
con: TMyConnection;
q: TMyQuery;
begin
con := TMyConnection.Create(nil);
q := TMyQuery.Create(nil);
try
con.Server := 'server';
con.Port := 3306;
con.Username := 'username';
con.Password := 'password';
con.Database := 'database';
con.LoginPrompt := False;
q.Connection := con;
q.SQL.Text := 'select * from tablename';
q.Open;
AResponseInfo.ContentText := '<html><head><title>My Response</title></head>' +
'<body>RecordCount: ' + IntToStr(q.RecordCount) + '</body></html>';
finally
q.Free;
con.Free;
end;
end;Re: Connection at runtime
Got that thanks.
How do I use the events.
ie the afterOpen
how would I do query.afterOpen -
if AddToMemo = 'Y' then begin memo1.Lines.Add('Connected'));
Cheers
SteveW
How do I use the events.
ie the afterOpen
how would I do query.afterOpen -
if AddToMemo = 'Y' then begin memo1.Lines.Add('Connected'));
Cheers
SteveW
-
AndreyZ
Re: Connection at runtime
Here is a code example:
Code: Select all
type
TForm1 = class(TForm)
IdHTTPServer1: TIdHTTPServer;
procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
private
{ Private declarations }
procedure MyQueryAfterOpen(DataSet: TDataSet);
public
{ Public declarations }
end;Code: Select all
procedure TForm1.MyQueryAfterOpen(DataSet: TDataSet);
begin
// your code
end;
procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
con: TMyConnection;
q: TMyQuery;
begin
con := TMyConnection.Create(nil);
q := TMyQuery.Create(nil);
try
con.Server := 'server';
con.Port := 3306;
con.Username := 'username';
con.Password := 'password';
con.Database := 'database';
con.LoginPrompt := False;
q.Connection := con;
q.SQL.Text := 'select * from tablename';
q.AfterOpen := MyQueryAfterOpen;
q.Open;
AResponseInfo.ContentText := '<html><head><title>My Response</title></head>' +
'<body>RecordCount: ' + IntToStr(q.RecordCount) + '</body></html>';
finally
q.Free;
con.Free;
end;
end;