2009-07-14 63 views
2

林具有这种功能,以确定天气用户在数据库中存在或不SQL返回值

DM是我DataModule的

AQ_LOGIN一个ADOQuery

贝努我的表填充有用户和他们的密码

这里谈到的代码:

function UserCheckExist(Login, pw: string): boolean; 
begin 
    with DM do 
    begin 
     AQ_LOGIN.Close; 
     AQ_LOGIN.SQL.Clear; 
     AQ_LOGIN.SQL.Add('select BLOGIN from BENU where BLOGIN = ''Login'' AND BPW = ''pw'''); 
     AQ_LOGIN.Open; 
    end; 
end; 

我现在的问题是: 如何使函数返回true或false天气具有相应密码的用户存在?

在此先感谢。

回答

3
function UserCheckExist(Login, pw: string): boolean; 
begin 
    with DM do 
    begin 
     AQ_LOGIN.Close; 
     AQ_LOGIN.SQL.Clear; 
     AQ_LOGIN.SQL.Add('select BLOGIN from BENU where BLOGIN = ''Login'' AND BPW = ''pw'''); 
     AQ_LOGIN.Open; 
     Result := (AQ_LOGIN.RecordCount > 0); 
     AQ_LOGIN.Close; 
    end; 
end; 
3

用途:

function UserCheckExist(Login, pw: string): boolean; 
begin 
    with DM do 
    begin 
    AQ_LOGIN.Close; 
    AQ_LOGIN.SQL.Clear; 
    {Use COUNT in select to determine if user exists} 
    AQ_LOGIN.SQL.Add('select count(BLOGIN) from BENU where BLOGIN = ''Login'' AND BPW 'pw'''); 
    AQ_LOGIN.Open; 
    Result:= (AQ_LOGIN.Fields[0].AsInteger = 1); 
    AQ_LOGIN.Close; 
end; 

末;

两个变化:首先,不要选择用户名,而应该计算值 - 如果没有用户存在,COUNT总是返回一些值 - 它将为零。 第二:如果count(Fields [0],因为没有更多字段存在)使用比较来计算结果等于1。如果这些记录的计数与一个不同,则此函数将返回false。

+1

从我+1,因为你张贴了我想要的东西。但是,我会使用参数而不是连接字符串输入 - 而且,因为发布的代码(在问题和其他答案中) - 它实际上不会工作吗? :-) – robsoft 2009-07-14 10:30:33

+0

@robsoft:我认为这段代码需要REAL重构... – smok1 2009-07-14 14:36:13

+0

@ smok1 - 的确如此 - 我认为他的代码只是让他感到困惑,因为他似乎还在努力使测试工作。 :-( – robsoft 2009-07-14 15:06:18

8

我会与smok1的答案(我只是发布类似的东西),但我会参数化您的输入,因此;

 
AQ_LOGIN.SQL.Add('select count(*) from BENU where BLOGIN=:login and BPW=:pw'); 
AQ_LOGIN.Parameters.ParamByName('login').AsString:=login; 
AQ_LOGIN.Parameters.ParamByName('pw').AsString:=pw; 

然后和smok1一样 - 打开数据集并查看返回的计数值。

NB - 没有一个ADO德尔福组件方便,但99.9%确定这是:-)语法

编辑:使用像这样的参数的优点之一是,你不必净化你的输入字符串(用于引号之类的东西) - 组件知道如何处理字符串。你不会期望有一个带有单引号的用户名,但你可能有一个密码。 :-)

1

您可以检查Eof。

function UserCheckExist(Login, pw: string): boolean; 
begin  
    with DM do  
    begin   
    AQ_LOGIN.Close;   
    AQ_LOGIN.SQL.Clear;   
    AQ_LOGIN.SQL.Add('select BLOGIN from BENU where BLOGIN = ' + QuotedStr(Login) + ' AND BPW = ' + QuotedStr(pw));   
    AQ_LOGIN.Open;   
    Result := (not AQ_Login.Eof); 
    AQ_LOGIN.Close;  
    end; 
end; 
0

我增加了一个检查,天气,用户是活动的。但它不能正常工作。

function UserCheck(Login, pw: string): boolean; 
    begin 
    with DM do 
    begin 
     AQ_LOGIN.Close; 
     AQ_LOGIN.SQL.Clear; 
     AQ_LOGIN.SQL.Add('select COUNT(*) from BENU where BLOGIN = ''Login'' AND BPW = ''pw'' AND AKTIV = 1'); 
     AQ_LOGIN.Open; 
     Result := (AQ_LOGIN.RecordCount > 0); 
     AQ_LOGIN.Close; 
    end; 
end; 

这是我使用的功能:

procedure TBenu_Login_Form.btnLoginClick(Sender: TObject); 
    var pwhashed: string; 
    begin 
    pwhashed := MD5Print(MD5String(edtBPass.Text)); 
    if UserCheck(meBLogin.Text, pwhashed) then 
     ShowMessage('User exists, Password is fine and active!') 
    else 
     ShowMessage('User does not exist, Password is wrong or not active!'); 
    end; 

想知道为什么,这并不为intendet工作。当我即输入一个不存在的用户名时,它总是返回UserCheck为真,从不为假。

0

由于您使用的是没有连接的adoquery组件,因此我假定数据库全部位于系统或链接的网络上。虽然人们总是认为只有sql可以工作,但是可以使用adotable.locate函数或adoquery.locate,尽管表字段必须在检索之前手动使其不安全。locate函数已经可以防止注入其参数,并且只根据找到的值返回布尔值。有些人可能会说不安全,我不知道你的应用程序,但它更快。