根据
连接方式、数据库类型、数据库服务器、数据库名称、登陆账号、密码
如何写一个测试连接的方法,测试是否可以连接的上数据库。

解决方案 »

  1.   

    begin
      if cmbUser.GetTextLen=0 then
      begin
        cmbUser.Text:='请输入用户名';
        cmbUser.SetFocus;
        MessageBeep(88);
        exit;
      end;
      if EditPassword.GetTextLen=0 then
      begin
        EditPassword.Text:='请输入密码';
        EditPassword.SetFocus;
        MessageBeep(88);
        exit;
      end;
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select count(*) from Login where 用户名='+
          QuotedStr(Trim(cmbUser.Text))+' and 密码='+ 
          QuotedStr(Trim(EditPassword.Text))+''); 
        Prepared;
        Open;
        if ADOQuery1.Fields[0].AsInteger=1 then
            begin
          showmessage('登录成功');
        end
        else
        begin
          count:=count+1;            //记录输错密码次数
          if count=3 then            //如果输入错三次
          begin
            Application.MessageBox('密码输入错误3次'+#13+'无法登录系统!','错误',
              MB_OK+MB_ICONERROR);
            Loginfrm.Close;
          end;
          Application.MessageBox(PChar('密码错误,重新输入'+#13+'还有'+
            inttostr(3-count)+'次机会'),'错误',MB_OK+MB_ICONERROR);
        end;
      end;
    end;