我是通过INI连接SQL2000数据库的,以下是代码:(但是运行时,第一次点button1正常,但当第二次点button1的时候,就会报错:打开对象时不允许操作...请问怎么处理呢?是什么原因?初学,谢谢)
procedure TForm1.Button1Click(Sender: TObject);
var
FileName,ServerIP,SQLDBName,SQLUserName,SQLPwd:string;
begin
   FileName:=ExtractFilePath(application.ExeName)+'conf.ini';
          with   TInifile.Create(Filename)   do
          begin
              ServerIP:=ReadString('ServerInfo','ServerIP','');
              SQLDBName:=ReadString('ServerInfo','SQLDBName','');
              SQLUserName:=ReadString('ServerInfo','SQLUserID','');
              SQLPwd:=ReadString('ServerInfo','SQLPwd','');
              Destroy;
          end;
   ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+SQLPwd+
          ';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog='
          +SQLDBName+';Data Source='+ServerIP;
   aa.Close;
   aa.SQL.Clear;
   aa.SQL.Add('select * from T');
   aa.Open;
end;

解决方案 »

  1.   


    procedure TForm1.Button1Click(Sender: TObject); 
    var 
    FileName,ServerIP,SQLDBName,SQLUserName,SQLPwd:string; 
    begin 
      FileName:=ExtractFilePath(application.ExeName)+'conf.ini';   with  TInifile.Create(Filename)  do 
      begin 
        ServerIP:=ReadString('ServerInfo','ServerIP',''); 
        SQLDBName:=ReadString('ServerInfo','SQLDBName',''); 
        SQLUserName:=ReadString('ServerInfo','SQLUserID',''); 
        SQLPwd:=ReadString('ServerInfo','SQLPwd',''); 
        Free;
      end; 
      
      ADOConnection1.close;
      ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+SQLPwd+ 
              ';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog=' 
              +SQLDBName+';Data Source='+ServerIP; 
      try
        ADOConnection1.open;  
        aa.Close; 
        aa.connection := ADOConnection1;
        aa.SQL.Clear; 
        aa.SQL.Add('select * from T'); 
        aa.Open; 
      except
        raise;
      end;
    end;
      

  2.   

    另外我想知道,dephi程序的入口在哪里?我想程序一启动就连接ini数据库,就不用每个控件事件都写这些代码
      

  3.   

    點 project ->View  Resource
    begin
      Application.Initialize;
      Application.MainFormOnTaskbar := True;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
      

  4.   

    ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+SQLPwd+ 
              ';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog=' 
              +SQLDBName+';Data Source='+ServerIP; 
    前面加 ADOConnection1.close;
      

  5.   

    不仅仅是 adoConnection,其它如adoDataset,在修改它的连接属性或CommandText时,都要先关闭对象,这是一个良好的编程习惯。
      

  6.   

    在你的程序基础上改,
    form.onshow:
    bgein
      Button1.visible:=false;
      Button1Click;
    end;
    就可以了.
      

  7.   

    要先关闭对象ADOConnection1,再打开
      

  8.   


    或在你主窗口formcreate的时候 设置数据库连接
      

  9.   


      在FormCreate里加上
      FileName:=ExtractFilePath(application.ExeName)+'conf.ini';   with  TInifile.Create(Filename)  do 
      begin 
        ServerIP:=ReadString('ServerInfo','ServerIP',''); 
        SQLDBName:=ReadString('ServerInfo','SQLDBName',''); 
        SQLUserName:=ReadString('ServerInfo','SQLUserID',''); 
        SQLPwd:=ReadString('ServerInfo','SQLPwd',''); 
        Free;
      end; 
      
      ADOConnection1.close;
      ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+SQLPwd+ 
              ';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog=' 
              +SQLDBName+';Data Source='+ServerIP;
      在
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      aa.Close;
      aa.SQL.Clear;
      aa.SQL.Add('select * from T');
      aa.Open;
    end;