窗体用到adoconnection和adoQuery控件。 
函数function TwinNeedLogin.checkconnect(s:string;ADOcon:TADOConnection):boolean; //用来返回连接数据库状态。
控件设置部分
adoQuery的connection属性设置为 ADOConnection   //貌似是连接起来了吧函数部分
function TwinNeedLogin.checkconnect(s:string;ADOcon:TADOConnection):boolean;
begin
  ADOcon:=TADOConnection.Create(nil);
  ADOcon.ConnectionString:='';
  ADOcon.Connected:=False;
  ADOcon.LoginPrompt:=False;
  ADOcon.ConnectionString:=s;
  try
    ADOcon.Connected:=True;
    ADOcon.Free;
    result:=True;
  except
    ADOcon.Free;
    result:=False
  end;
end;
调用函数部分procedure TwinNeedLogin.FormCreate(Sender: TObject);
begin
  //初始化程序,连接服务器SQL数据库
  if checkconnect(
                  'Provider=SQLOLEDB.1;'           +
                  'Password=110119120;'              +
                  'Persist Security Info=True;'    +
                  'User ID=sa;'                    +
                  'Initial Catalog=BEAN;'          +
                  'Data Source=127.0.0.1'
  ,ADOConnection) then
  //成功链接数据库
    showmessage('数据库连接成功')  else
    showmessage('数据库连接错误');
end;----------------------------------》调用函数后,提示数据库连接成功,没问题《--------------------------------
当想查询数据的时候
procedure TwinNeedLogin.sButtonLoginClick(Sender: TObject);
begin
  try
    ADOQuery.Close;
    ADOQuery.SQL.Add('select * from user');
    ADOQuery.SQL.Add('where user="mostar"');
    ADOQuery.Open;
  finally
    ADOQuery.Close;
  end;
end;报错,说是丢失连接或连接字符串我查了查CSDN和Google   有相关问题,都是连接上的问题,adoquery和adoconnection的连接。  我是动态加载,会有什么区别吗

解决方案 »

  1.   

    你这两段函数根本没有直接关系吗
    adoQuery的connection属性设置为 ADOConnection 但是你的代码中好像没有对adoconnection的操作吧你checkconnect函数虽然与数据库建立了连接,但是你的adoconnection控件是自动创建的,而且测试完成后就释放了
      

  2.   

    加句代码
    ADOQuery.connection := ADOcon;
      

  3.   


    function TwinNeedLogin.checkconnect(s:string;ADOcon:TADOConnection):boolean;
    begin
      ADOcon:=TADOConnection.Create(nil);
      ADOcon.ConnectionString:='';
      ADOcon.Connected:=False;
      ADOcon.LoginPrompt:=False;
      ADOcon.ConnectionString:=s;
      try
      ADOcon.Connected:=True;
      //ADOcon.Free;//这句代码屏蔽掉
      result:=True;
      except
      //ADOcon.Free;//还有这句
      result:=False
      end;
    end;
    估计这样就可以了
      

  4.   

    function TwinNeedLogin.checkconnect(s:string;ADOcon:TADOConnection):boolean;
    begin
     // ADOcon:=TADOConnection.Create(nil);
      ADOcon.ConnectionString:='';
      ADOcon.Connected:=False;
      ADOcon.LoginPrompt:=False;
      ADOcon.ConnectionString:=s;
      try
      ADOcon.Connected:=True;
      //ADOcon.Free;
      result:=True;
      except
      ADOcon.Free;
      result:=False
      end;
    end;
      

  5.   

    1:首行函数里面创建的
    ADOcon.Connected:=True;
      ADOcon.Free;
    不应该立刻释放掉,应该在关闭或是程序退出里释放,因为这是一个全局的数据连接2:sButtonLoginClick在这个方法查询的时候
    你最声码应该把 ADOQUERY 与 ADOcon连接吧。。
      

  6.   

    ADOQuery.SQL.Text:='select * from clientuser where username="'+   sCobLogin.Text+   '" and psd="'+   sEditPwd.Text+   '"';
    这句报错, 说是列名“mostar”无效。
    sCobLogin.Text 的值为  mostar
    sEditPwd.Text  的值为  12345数据库响应的username和psd都是字符型错误是不是在引号上?我看有的用双引,有的单引
      

  7.   

     ADOcon.Free;你把它FREE掉了,当然出错了。
      

  8.   

    行了  这是什么意思呢  delphi有没有转义?