Page 1 of 1

Connection at runtime

Posted: Mon 14 May 2012 19:48
by classic12
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

Re: Connection at runtime

Posted: Tue 15 May 2012 07:24
by AndreyZ
Hello,

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

Posted: Tue 15 May 2012 20:53
by classic12
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

Re: Connection at runtime

Posted: Wed 16 May 2012 10:39
by AndreyZ
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;