做的系统实现多用户分别对应不同的数据库,连接的控件是ADOQUERY,放在DATAMODULE中,因为开始用ADOCONNECTION动态连接做不出来,只好光用ADOQUERY连接了,但现在的问题是在删除数据时说数据正在使用中,我的分离代码如下:
datamodule1.ADOQueryborrow.Close;
with datamodule1.ADOQueryuser do
  begin
    sql.Clear;
    active:=false;
    connectionstring:='Provider=SQLOLEDB.1;'
    +'Integrated Security=SSPI;Persist Security Info=False;'
    +'Initial Catalog=financing';
  end;
financing是另一个数据库,我是先让它连接上另一个数据再去删我想删除的那个,可总是说在使用中,分离代码错在什么地方

解决方案 »

  1.   

    第一句写错了,是
    datamodule1.ADOQueryuser.Close;
      

  2.   

    with datamodule1.ADOQueryuser do
      begin
        close;//这里关闭
         sql.Clear;
        //active:=false;//这个不要了
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=financing';
      end;
      

  3.   

    SORRY~试了,还是不行,连接时也是用这些代码,断开却不行了,晕死~~~~~
      

  4.   

    是不是你其他dataset或query还没有close掉。所有就无法断开。你仔细检查一下看看。或者你新建一个 application 只用一个query和connnetion试一下能不能断开。
      

  5.   

    有一个DATAMODULE,之下有一个ADOCONNECTION,ADOQUERY,DATASOURCE连接主数据库,不是动态连接,另外有六个ADOQUERY和DATASOURCE连接和用户对应的数据库,是动态连接的,分离和删除的是用户对应的数据库,分离代码
    procedure Tdeletefrm.FormCreate(Sender: TObject);
    begin
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //动态分离数据库
    with datamodule1.ADOQuerytry do
      begin
        close;
        sql.Clear;
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=financing';
      end;
    with datamodule1.ADOQueryuser do
      begin
        close;
        sql.Clear;
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=financing';
      end;
    with datamodule1.ADOQuerybank do
      begin
        close;
        sql.Clear;
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=financing';
      end;
    with datamodule1.ADOQueryinout do
      begin
        close;
        sql.Clear;
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=financing';
      end;
    with datamodule1.ADOQuerysort do
      begin
        close;
        sql.Clear;
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=financing';
      end;
    with datamodule1.ADOQueryborrow do
      begin
        close;
        sql.Clear;
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=financing';
      end;
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //动态分离数据库删除数据库代码,n是变量,加载和用户名相同的数据库,加载进的数据库非要删除的数据库:
    try
    with datamodule1.ADOQuery do
      begin
        close;
        sql.Clear;
        sql.Add('drop database '+n);
        execsql;
      end;
    with datamodule1.ADOQuery do
      begin
        close;
        sql.Clear;
        sql.Add('delete from userbiao where uname=:a');
        parameters.ParamByName('a').Value :=n;
        execsql;
      end;
    application.MessageBox('帐户删除成功,系统将返回初始状态!','提示,64');
    close;
    except
    application.MessageBox('帐户删除失败!','提示',64);
    end;
      

  6.   

    另做了一个试验,一个FORM,一个DATAMODULE
    DATAMODULE之下一个ADOQUERY,一个DATASOURCE,
    FORM之下两个按钮,BUTTON1(删除数据库),BUTTON2(分离数据库)
    数据库名为:DA
    代码如下:
    //分离~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    with datamodule2.ADOQuery1 do
      begin
        close;
        sql.Clear;
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=master';
      end;
    end;
    //删除~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    with datamodule2.ADOQuery1 do
      begin
        close;
        sql.Clear;
        sql.Add('drop database da');
        execsql;
        showmessage('删除成功!');
      end;
    end;
    先点分离按钮,再点删除按钮,结果跳出数据库正在使用,无法删除!!!
    !!!!!!!!!不会是人品问题吧~~~~~~~~~
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    with datamodule2.ADOQuery1 do
      begin
        close;
        sql.Clear;
        sql.Add('drop database da');
        execsql;
        showmessage('删除成功!');
      end;
    end;
    //上面当然会提示正在使用了,SQL对连接会自已记数的,但即使所有的用户都断开,它也不一定完全清除,您打开企业管理器-->找到要删除的数据库-->点分离数据库-->确定-->使用本数据库的连(一点大于0)-->否
    现在您可以删除了
      

  8.   

    datamodule1.ADOQueryborrow.Close;
    with datamodule1.ADOQueryuser do
      begin
        sql.Clear;在这边加一句
        aql.add('use mastr');   // 让mastr成为当前数据库,你就可以对其他数据库进行操作了。我在作数据库还原时问题差不多,后来就是这样解决的。
       
        connectionstring:='Provider=SQLOLEDB.1;'
        +'Integrated Security=SSPI;Persist Security Info=False;'
        +'Initial Catalog=financing';
      end;
      

  9.   

    用sql 的查询分析器查看一下
      

  10.   

    Call Close to set the Active property of a dataset to false. When Active is false, the dataset is closed; it cannot read or write data and data-aware controls can抰 use it to fetch data or post edits.
    close不是断开连接,关闭数据集而已试试ADOQuery1.Connection.Close;