简体   繁体   English

Delphi / ADO:如何获得Execute()的结果?

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

I have declared AdoConnection : TADOConnection; 我已经宣布了AdoConnection : TADOConnection; and successfully connected to the default "mysql" database (so, no need to pass that code). 并成功连接到默认的“mysql”数据库(因此,无需传递该代码)。

Now, taking baby steps to learn, I would like to AdoConnection.Execute('SHOW DATABASES', cmdText); 现在,采取宝宝步骤学习,我想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 that I don't know how I can examine the result of the command :-/ 这似乎工作正常,从某种意义上说它不会抛出异常,但我是一个n00b,我不知道如何检查命令的结果: - /

Halp! 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. @mawg, SHOW DATABASES命令返回一个名为'Database'的列的数据集,因此您可以使用TADOQuery组件来读取数据。

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 . 你需要的是到达返回的_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 TADOCommand s' Execute methods). 如果您不介意使用Delphi组件,则应使用TADODataSetTADOQuery来执行可返回任何结果的SQL命令(或更低级别的TADOCommand的' Execute方法)。
If you wanty something realy simple, w/o all the VCL balast, you mignt want to try the TADOWrap class from ExplainThat (MIT licenced). 如果你想要一些非常简单的东西,没有所有的VCL balast,那么你想要从ExplainThat (MIT许可)尝试TADOWrap类。

You must use an TADOQuery connected to TADODataBase. 您必须使用连接到TADODataBase的TADOQuery。 At property SQL you must include the SQl statament to retrive databases in SGDB. 在属性SQL中,您必须包含SQl statament以在SGDB中检索数据库。 In SQL Server you can do this: 在SQL Server中,您可以这样做:

SELECT * FROM sysdatabases SELECT * FROM sysdatabases

In MySQL must exist something similar to retrieve the names. 在MySQL中必须存在类似于检索名称的东西。

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): 您可以将此TADOQuery连接到TDataSource和DBGrid以查看可视结果或使用代码来探索查询结果(有些类似于此):

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

Regards 问候

If you simply want to populate a grid why dont you use adotable ? 如果您只想填充网格,为什么不使用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. 然后,您可以让任何UI访问数组或变体。 Or populate a dataset. 或填充数据集。

您需要TAdoQuery来执行返回结果的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM