unit dASP;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,ComObj, StdCtrls, ExtCtrls, Grids, DBGrids, DB,ADODB;type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    DBGrid1: TDBGrid;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
        AConnection,ARecordSet:variant;
        longintTemp : integer;
        strTemp,strSQL: string;
        intIndex : integer;
begin
        //strSQL:='StuInfo';
        strSQL:='SELECT 学号,姓名,性别 FROM StuInfo';
        AConnection:=CreateOleObject('Adodb.connection');
        //AConnection.Open('Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\Sample\20040710\Student');
        AConnection.Open('DSN=qlTEST');
        ARecordSet:=CreateOleObject('Adodb.recordset');
        ARecordSet.open(strSQL,AConnection);
        //Self.DBGrid1.DataSource:=ARecordSet;(为什么这句话不可以?)
        ARecordSet.close;
        AConnection.close;
end;end.
我想通过程序用ADODB动态连接Access数据库,生成的纪录集送给DBGrid.DataSource,为什么不可以?应该怎么写,注意我的控件是DBGrid,不是StringGrid,不要告诉我借用AdoConnection控件和AdoDataSet控件,我的目的是想通过程序将查询出来的纪录集送到DBGrid中,我个人不习惯在设计阶段去设置那些控件的属性,我感觉很不爽?

解决方案 »

  1.   

    我认为这是不行的,因为DELPHI的ADO控件是对ADO对象进行了封装,除非你好好的看看VCL的帮助,参照里面的方法。
      

  2.   

    既然你不愿意使用绑定控件,为什么还要强行给人家输入呢?DBGrid必须是要有数据源的,如果你想自己处理为什么偏偏不用StringGrid? 真受不了你。
      

  3.   

    其实你可以使用ADOQuery啊,在里面写SQL语句然后执行,DBGrig里自然就根据你的语句而变化啦!
      

  4.   

    把DBGRID连接到一个空的ADODATASET组件上,然后把你取得的adodataset接口赋给adodataset.Recordset