2009-07-14 85 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函數已經可以防止注入其參數,並且只根據找到的值返回布爾值。有些人可能會說不安全,我不知道你的應用程序,但它更快。