如何使用ADO连接ACCESS数据库并打开一个记录集。我不希望使用ODBC,而是使用connectionstring。
假设有一个数据库MyDB,其中有个表TableA;需要得到一个datasource为'select * from TableA'的记录集对象RecordsetA。具体该怎么做,望给以代码示范。
我想方法应该有多种,方法越多越好,便于学习。
我以前是用VB,新转DELPHI,望指点。

解决方案 »

  1.   

    双击AdoConnection->Build->MicroSoft Jet 4.0 Ole DB provier
      

  2.   

    我的一段代码是这样的,知道如何Connect,但不知道如何打开recordset.
      var
        myaccess: string;
        strSql: string;
        recA: TRecordset;
        cnn: TConnection;
      const
        mystr1='Provider=%s;Data Source=%s;Persist Security Info=false;Jet OLEDB:Database Password=%s';
        myprovider='Microsoft.Jet.OLEDB.4.0';
        mypassword='AAA';
    begin
      opendialog1.filename:='*.mdb';
      if opendialog1.execute then
      begin
        //打开数据库连接
        myaccess:=opendialog1.FileName;
        cnn:=TConnection.Create(self);
        if cnn.State=adStateOpen then
          cnn.Close;
        cnn.ConnectionString:=format(mystr1 ,[myprovider,myaccess,mypassword]);
        cnn.CommandTimeout:=30;
        cnn.CursorLocation:=adUseClient;
        cnn.Open('','','',-1);
        //打开记录集
        strSql:='select * from songs';
        recA:=TRecordset.Create(self);
        {
        ???
        到这里就不知道怎么办了...
        如果用recA.open(),又提示没有该方法。
        我用VB的时候,可以肯定recordset对象有open方法,但为什么在delphi中就没有该方法了呢?
        }
      end;
    end;
      

  3.   

    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\My Documents\test.mdb;Persist Security Info=False
    类似上面的就是connectionstring,当然你可以动态赋值。
    动态赋值过程也可以写到BeforeConnect过程里面
      

  4.   

    我不希望用可视化控件,也知道如何设置connectionstring。
    希望各位能给予具体的代码以说明。
      

  5.   

    foxyy8888:
    连接没问题,问题是如何open recordset
      

  6.   

    Delphi里面把ADO封装好了,根本不用TRecordset。
    直接用AdoDataSet(AdoDataSet,AdoTable,AdoQuery)就行了。
    你可以在设计期就将他们连接上(Connetion属性,如果是AdoTable要用TableName选择连接的表,AdoQuery要写SQL语句),然后DataSource连接到AdoDataSet,再连接一个DBGrid就完全把数据表里面的数据全部显示出来。
      

  7.   

    建议先看看书。
    这是Delphi里面标准数据库连接。
    就是数据绑定。
      

  8.   

    我本身知道一种办法可行,但我觉得该办法有点麻烦。就是在Delphi的编辑器中,不能自动弹出提示(如输入了.后,可以自动弹出该对象的方法和属性)。
      var
        DBFile: string;
        strSql: string;
        recA,cnn: variant;
      const
        mystr1='Provider=%s;Data Source=%s;Persist Security Info=false;Jet OLEDB:Database Password=%s';
        myprovider='Microsoft.Jet.OLEDB.4.0';
        mypassword='AAA';
    begin
      opendialog1.filename:='*.mdb';
      if opendialog1.execute then
      begin
        //打开数据库连接
        DBFile:=opendialog1.FileName;
        cnn:= CreateOleObject('ADODB.Connection');
        cnn.ConnectionString:=format(mystr1 ,[myprovider,DBFile,mypassword]);
        cnn.Open('','','',-1);
        if cnn.State=adStateOpen then
          showmessage('connected');
          
        //打开记录集
        strSql:='select * from songs';
        recA:= CreateOleObject('ADODB.RecordSet');
        recA.open(strSql,cnn,adOpenStatic,adLockOptimistic,adCmdText);    showmessage(inttostr(recA.recordcount));
        recA.Close;  end;
      

  9.   

    foxyy8888:
    建议你先看清问题,我不希望使用可视化控件,我想使用ADO的标准化组件来实现。
      

  10.   

    ADO组建本身就是通过OLE DB驱动数据的,完全脱离ODBC,BDE.
    如果你非要自己写,可以参照SOURCE\VCL\ADODB.PAS,看看它是怎么封装的。
      

  11.   

    直接用控件就好了,只要加一句sql代码就行了
      

  12.   

    foxyy8888:
    你的意思是:delphi中的ADO控件,将MDAC中的ADO组件又进行了二次封装,是这个意思吧?
      

  13.   

    对,数据访问方式就是基本和你的思路一样。在封装上更符合VCL风格而已。
      

  14.   

    嘿,明白了。
    而且我试了一下,使用delphi的ADO,在功能实现上,代码要比原生的ADO还要简单些。
      

  15.   

    Delphi里面把ADO封装好了,根本不用TRecordset。  
    直接用AdoDataSet(AdoDataSet,AdoTable,AdoQuery)就行了。
    ------------------
    这是解决我问题的关键所在。