1、
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conn.connectionstring="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=StoreHouseMaster;Data Source=IBM"
conn.open
rs.open "select * from operator where user_name='" & username.text & "' and userpassword='" & userpassword.text & "'",conn
if rs.eof then
msgbox "密码错误!"
else
msgbox "密码正确!"
end if
2、
上面的代码有安全性问题啊
可以在username里面输入字串“1 or 1=1 --”就可以跳过密码检测
rs.open "select userpassword from operator where user_name='" & username.text & "'"
if rs(0)=userpassword.text then
msgbox "密码错误!"
else
msgbox "密码正确!"
end if
还要考虑用户不合法的情况:
rs.open "select userpassword from operator where user_name='" & username.text & "'"
if rs.recordcount=0 then
msgbox "用户不存在!"
elseif rs(0)<>userpassword.text then
msgbox "密码错误!"
else
msgbox "密码正确!"
end if
3。
Private Sub Command1_Click() '登陆
On Error GoTo Err
If UserName.Text = "" Then
MsgBox "请输入操作员!", vbInformation, "提示"
Exit Sub
End If
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=StoreHouseMaster;Data Source=IBM"
cn.Open
rs.CursorLocation = adUseClient
Dim name As String, passa As String
name = Trim(UserName.Text)
passa = Trim(userpassword.Text)
rs.Open "select * from Operator where user_name='" & name & "'", cn, adOpenKeyset, adLockReadOnly
If rs.EOF Then
MsgBox "该用户尚未注册!", vbOKCancel, "提示"
UserName.SetFocus
UserName.SelStart = 0
UserName.SelLength = Len(UserName.Text)
Exit Sub
Else
If passa <> Trim(rs!userpassword) Then
MsgBox "密码不正确,请重输!!!", vbQuestion, "提示"
userpassword.Text = ""
userpassword.SetFocus
Exit Sub
End If
Me.Hide
main.Show 'main为主窗口名称
Exit Sub
Err:
MsgBox Err.Description
End Sub