开发者

Delphi / ADO : how to get result of Execute()?

I have declared AdoConnection : TADOConnection; and successfully connected to the default "mysql" database (so, no need to pass that code).

Now, taking baby steps to learn, I would like to AdoConnection.Execute('SHOW DATABASES', cmdText); which seems to work ok, in the sense that it doesn't throw an exception, but I am such a n00b th开发者_高级运维at I don't know how I can examine the result of the command :-/

Halp!


@mawg, the SHOW DATABASES command returns an dataset with one column called 'Database', so you can use the TADOQuery component to read the data.

try this code.

var
  AdoQuery : TADOQuery;
begin
   AdoQuery:=TADOQuery.Create(nil);
   try
    AdoQuery.Connection:=AdoConnection;
    AdoQuery.SQL.Add('SHOW DATABASES');
    AdoQuery.Open;
    while not  AdoQuery.eof do
    begin
      Writeln(AdoQuery.FieldByname('DataBase').AsString);
      AdoQuery.Next;
    end;
   finally
   AdoQuery.Free;
   end;


end;


What you need is to reach the returned _Recordset.
If you don't mind using the Delphi components, you should use TADODataSet or TADOQuery to execute a SQL command which can return any results (or the more low-evel TADOCommands' Execute methods).
If you wanty something realy simple, w/o all the VCL balast, you mignt want to try the TADOWrap class from ExplainThat (MIT licenced).


You must use an TADOQuery connected to TADODataBase. At property SQL you must include the SQl statament to retrive databases in SGDB. In SQL Server you can do this:

SELECT * FROM sysdatabases

In MySQL must exist something similar to retrieve the names.

You can connect this TADOQuery to a TDataSource and a DBGrid to see visual result or use code to explore the result of the query (some similar to this):

ADOQuery1.Open;
while not ADOQuery1.eof do begin
  Name := ADOQuery1.FieldByName('DBName').AsString;
  ADOQuery1.Next;
end; 

Regards


You need TAdoQuery to execute statements, that return results.


If you simply want to populate a grid why dont you use adotable ?

Adotable.open;
Adotable.first;
While NOT adotable.eof do(not sure about not or <>)
Begin
Put values in variant or array;

Adotable.next;
End

Then you can let any UI access the array or variant. Or populate a dataset.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜