unit U_AdoQuery;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Buttons, DBClient, ExtCtrls,SQLEdit, Grids,
DBGrids;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
QueryButton: TButton;
EditCommandText: TSpeedButton;
ADOQuery1: TADOQuery;
SQLText: TMemo;
Connection: TADOConnection;
DBGrid1: TDBGrid;
Label1: TLabel;
procedure QueryButtonClick(Sender: TObject);
procedure EditCommandTextClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.QueryButtonClick(Sender: TObject);
begin
AdoQuery1.Close;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add(SQLText.Text);
AdoQuery1.Open;
end;
procedure TForm1.EditCommandTextClick(Sender: TObject);
var
Command: string;
begin
Command := SQLText.Text;
if EditSQL(Command, Connection.GetTableNames, Connection.GetFieldNames) then
SQLText.Text := Command;
end;
end.
unit U_AdoCommand;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids;
type
TF_AdoCommand = class(TForm)
ADOConnection1: TADOConnection;
ADOCommand1: TADOCommand;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADODataSet1: TADODataSet;
ADOCommand2: TADOCommand;
ExcuteButton: TButton;
CancelButton: TButton;
DataSource2: TDataSource;
DBGrid2: TDBGrid;
CancelAllButton: TButton;
ADODataSet2: TADODataSet;
ExcuteAllButton: TButton;
ComboBox1: TComboBox;
Label1: TLabel;
procedure ExcuteButtonClick(Sender: TObject);
procedure CancelButtonClick(Sender: TObject);
procedure CancelAllButtonClick(Sender: TObject);
procedure ExcuteAllButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
F_AdoCommand: TF_AdoCommand;
implementation
{$R *.dfm}
procedure TF_AdoCommand.ExcuteButtonClick(Sender: TObject);
begin
if Combobox1.ItemIndex =0 then
//调用第一个ADO命令组件的Excute方法,并在DBGrid1中显示查询结果
with ADOCommand1 do
begin
CommandType := cmdText;
CommandText :='select * from country';
ADODataSet1.Recordset := Execute;
end
else
//调用第二个ADO命令组件的Excute方法,并在DBGrid2中显示查询结果
with ADOCommand2 do begin
CommandType := cmdText;
CommandText :='select * from employee';
ADODataSet2.Recordset := Execute;
end;
end;
procedure TF_AdoCommand.CancelButtonClick(Sender: TObject);
begin
if Combobox1.ItemIndex =0 then
begin
ADOCommand1.Cancel;
ADODataSet1.Recordset :=nil ;
end
else
begin
ADOCommand2.Cancel;
ADODataSet2.Recordset :=nil;
end;
end;
procedure TF_AdoCommand.CancelAllButtonClick(Sender: TObject);
begin
ADOCommand1.Cancel;
ADOCommand2.Cancel;
ADODataSet1.Recordset :=nil;
ADODataSet2.Recordset :=nil;
end;
procedure TF_AdoCommand.ExcuteAllButtonClick(Sender: TObject);
var
i: Integer;
begin
ADOCommand2.CommandText :='Update';
for i := 0 to (ADOConnection1.CommandCount-1) do
begin
if i=0 then
begin
ADOConnection1.Commands[i].CommandType := cmdText;
ADOConnection1.Commands[i].CommandText :='select * from country';
ADODataSet1.Recordset := ADOConnection1.Commands[i].Execute;
end
else if i=1 then
begin
ADOConnection1.Commands[i].CommandType := cmdText;
ADOConnection1.Commands[i].CommandText :='select * from employee';
ADODataSet2.Recordset := ADOConnection1.Commands[i].Execute;
end;
end;
end;
procedure TF_AdoCommand.FormCreate(Sender: TObject);
var
i: Integer;
begin
ComboBox1.Items.Clear;
//添加的列表项的数目等于ADOConnection1组件所相关的TADOCommand组件的个数
for i := 0 to (ADOConnection1.CommandCount-1) do
//添加的列表项为相关TADOCommand组件的名称
Combobox1.Items.Add(ADOConnection1.Commands[i].Name) ;
Combobox1.ItemIndex :=0;
end;
end.
应用TADOConnection、TADODataSet和TADOCommand组件
最新推荐文章于 2022-06-28 17:27:51 发布
本文介绍使用 ADO (ActiveX Data Objects) 进行数据库操作的方法,包括执行 SQL 查询、更新数据等常见操作。通过两个具体示例展示了如何在 Delphi 或 C++Builder 中利用 ADOQuery 和 ADOCommand 组件实现数据检索与交互。
98

被折叠的 条评论
为什么被折叠?



