今天做使用者登入的時候,智障了……
---------------------------------------------------------------------------------
友情提示:
登入的時候總提示密碼錯誤,
Print IIf(Trim(rs.Fields("password")) = password, "相等", "不相等")
找了好久,才發現:
SQL2000 設計資料庫的時候,欄位中,字串要設計成可變長度(varchar),
如果設計成char固定長度,那麼,如果你的字串不夠長度,資料庫會自動用空格補齊。
---------------------------------------------------------------------------------
VB6—>填加表單—>儲存為Login.frm
添加兩個標籤:
lable1.Caption="使用者名稱:"
lable2.Caption="密碼:"
添加兩個文字框:
text1命名為txtUser
text2命名為txtPass
Login.frm代碼如下:
1 '使用者登入代碼
2 Public Sub userlogin()
3
4 If txtUser.Text = "" Then
5 MsgBox "使用者名稱不可為空白"
6 txtUser.SetFocus
7 Exit Sub
8 End If
9 If txtPass.Text = "" Then
10 MsgBox "密碼不可為空白"
11 txtPass.SetFocus
12 Exit Sub
13 End If
14
15 username = Trim(txtUser.Text)
16 password = Trim(txtPass.Text)
17
18 Call OpenConn
19
20 sql = "select * from vuinfo where username='" & username & "'"
21 rs.Open sql, conn, 1, 1
22 '判斷使用者是否存在
23 If rs.EOF Then
24 MsgBox "使用者名稱不存在"
25 txtUser.SetFocus
26 Call CloseConn
27 Exit Sub
28 End If
29 '判斷使用者密碼
30 If rs.Fields("password") <> password Then
31 MsgBox "密碼錯誤"
32 Call CloseConn
33 Exit Sub
34 Else
35 username = rs.Fields("username")
36 password = rs.Fields("password")
37 userPower = rs.Fields("userPower")
38 Call CloseConn
39 infop.Show '顯示管理介面
40 Unload Login '卸載登入表單
41 End If
42 End Sub