procedure TForm1.Button1Click(Sender: TObject);
begin
count:=count+1;
with query1 do
begin
close;
sql.clear;
sql.add('select * from user');
sql.add('where id=:username'); id用户名字段
sql.add('and paw=asswd'); paw密码字段
parambyname('username').asstring:=edit1.text;
parambyname('passwd').asstring:=edit2.text;
prepare;
open;
end;
if mydata.paw.recordcount=0 then
begin
MessageDlg('登录失败!请检查用户名/密码!', mtInformation,[mbOk], 0);
if count=3 then
form1.close;
end
else
begin
halt;
end;
首先在数据库中定义一个存储过程:(这样可以增加数据访问的速度)
Create Procedure CheckUser
@userid varchar(10),
@password varchar(10)
as
select userid from userlist where userid=@userid and password=@password
GO
然后在IDE下增加如下代码:
procdure TForm1.Check(UserName,Passwd:String);
begin
with adoqy do begin
close;
sql.clear;
sql.add('exec CheckUser :ht,:ht1');
Parameters.parameterbyname('ht').values:=UserName;
Parameters.parameterbyname('ht1').values:=Passwd;
sql.execsql;
if recordcount<1 then begin showmessage('登陆失败!');exit; end;
end;
end;
//调用
procedure TForm1.Button1Click(Sender: TObject);
begin
procedure Check('小丽','123456');
end;