在密码的Edit2的OnEnter事件中写  Query1.Active:=False;
  Query1.Sql.Clear;
  Query1.sql.add('select * from user where Name='#39+edit1.text+#39);
  Query1.active:=True;
  if query1.recordcount<=0 then
    showmessage('No this User'+' '+edit1.text);

解决方案 »

  1.   

    li_zhifu:你的代码写得没错,但这种写法存在一个安全漏洞,
    如果以这种方法判断的话,我不需要知道数据库中的任何用户名或密码便可登入。
      

  2.   

    我这只是回答marrylove的问题,至于要登录,marrylove自然还要对密码进行验证.
      

  3.   

    我建议这样写:在输用户名的Edit中写代码procedure TLoginForm.UserEditKeyPress(Sender: TObject; var Key: Char);
    begin
      if key=#13 then
       begin
         with Query1 do
           begin
            Close;
            SQL.Clear;
            SQL.Add('Select *');
            SQL.Add('From 表 别名');
            SQL.Add('Where 条件');
            ParamByName('cn').AsString:=UserEdit.Text;
            Open;
            if recordcount<>0 then PasswordEdit.SetFocus
            else
             begin
              if MessageDlg('没有该用户!是否继续?',mtConfirmation,
                             [MBYes,MBNo],0)=mrYes then
               begin
                UserEdit.Clear;
                UserEdit.SetFocus;
               end;    
             end;
            Close
           end;
       end;
    end;
      

  4.   

    假如marrylove的密码如下判断那就完蛋啦,
    'select * from user where Name='#39+edit1.text+#39 + ' and password='#39+edit2.txt+#39
      

  5.   

    li_zhifu是对的,有几点可以商榷:
    1 是否把代码放在 Username 控件的 OnExit 事件上,这样只要焦点离开了UserName输入区
      就执行核查,逻辑上更好一些,我这么认为的,:-)
    2 Query1.sql.add('select count(Name) from user where Name='#39+edit1.text+#39);
      Query1.active:=True;
      if query1.Fields[0].AsInteger = 0 then
        showmessage('No this User'+' '+edit1.text); 
      这样就不用将整条记录返回来了,只需要取回感兴趣的内容就可以了...