unit ado;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Grids, DBGrids, ADODB, StdCtrls;type
  Tmain = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  main: Tmain;implementation{$R *.dfm}procedure Tmain.Button1Click(Sender: TObject);
const
  part1='Provider=MSDASQL;Persist Security Info=False;Extended Properties="DBQ=';
  part2=';DefaultDir=';
  part3=';Driver={Driver do Microsoft Access (*.mdb)};DriverId=25;FIL=MS Access;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"';
var
  appPath:string;
begin
  if(button1.Caption='连接') then
    begin
      appPath:=extractFilePath(application.ExeName);
      ADOConnection1.ConnectionString:=part1+appPath+'db.mdb'+part2+appPath+part3;
      ADOConnection1.Open;
      ADOQuery1.SQL.Text:='select * from users';
      ADOQuery1.Open;
      button1.Caption:='断开'
    end
  else
    begin
      button1.Caption:='连接'
      ADOQuery1.Close;        //提示这句有错误,为什么?!!!
      ADOConnection1.Close;
    endend;

解决方案 »

  1.   

    关键语句:
     if(button1.Caption='连接') then
        begin
          appPath:=extractFilePath(application.ExeName);
          ADOConnection1.ConnectionString:=part1+appPath+'db.mdb'+part2+appPath+part3;
          ADOConnection1.Open;
          ADOQuery1.SQL.Text:='select * from users';
          ADOQuery1.Open;
          button1.Caption:='断开'
        end
      else
        begin
          button1.Caption:='连接'
          ADOQuery1.Close;        //提示这句有错误,为什么?!!!
          ADOConnection1.Close;
        endend;
      

  2.   

    ADOQuery1.Close;        //提示这句有错误,为什么?!!!
    ------------------
    这句没有问题
    用Microsoft Jet 4.0吧, 应该不会有问题的
      

  3.   

    你的ADOConnection1.ConnectionString最好放在外边。
    在执行BUTTON的click事件时,AdoConnection1.open/close就行了。
    你出错的地方是不是你的ADOQUERY没有connection值吧。
      

  4.   

    在没加如关闭代码前我测试过ADO打开,没问题。
      

  5.   

    procedure Tmain.Button1Click(Sender: TObject);
    begin
      ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;'+'Data Source=db.mdb;Persist Security Info=False';
      if(button1.Caption='连接') then
        begin
          ADOConnection1.Open;
          ADOQuery1.SQL.Text:='select * from users';
          ADOQuery1.Open;
          button1.Caption:='断开'
        end
      else
        begin
          button1.Caption:='连接'
          ADOQuery1.Close;      //依旧是关闭错误!!???
          ADOConnection1.Close;
        endend;
    end.
      

  6.   

    procedure Tmain.Button1Click(Sender: TObject);
    begin
      ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;'+'Data Source=db.mdb;Persist Security Info=False';
      ADOQuery1.SQL.Text:='select * from users';
      if(button1.Caption='连接') then
        begin
          ADOConnection1.Open;
          ADOQuery1.Open;
          button1.Caption:='断开'
        end
      else
        begin
          ADOQuery1.Close;
          ADOConnection1.Close;
          button1.Caption:='连接'
        endend;
    end.调试通过,但在运行EXE文件时,ADO可以打开,段断开时出现异常“对象打开,操作不被允许”,这是为什么??
      

  7.   

    procedure Tmain.Button1Click(Sender: TObject);
    begin
      ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;'+'Data Source=db.mdb;Persist Security Info=False';
      ADOQuery1.SQL.Text:='select * from users';
      if(button1.Caption='连接') then
        begin
          ADOConnection1.Open;
          ADOQuery1.Open;
          button1.Caption:='断开';    //后面加一个分号
        end
      else
        begin
          ADOQuery1.Close;
          ADOConnection1.Close;
          button1.Caption:='连接'
        endend;
    end.