我SQL SERVER建了一个表(operator)有三个字段(user_name,userpassword,user_level)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"VB中有2个文本框(username,userpassword)求验证数据库里用户名和密码是否正确的代码?
小弟是初学者,求写详细点~谢谢!

解决方案 »

  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 & "'"if rs.eof then
        msgbox "密码错误!"
    else
        msgbox "密码正确!"
    end if
      

  2.   

    更正一下:
    rs.open "select * from operator where user_name='" & username.text & "' and userpassword='" & userpassword.text & "'",conn
      

  3.   

    rs.open "select * from operator where user_name='" & username.text & "' and userpassword='" & userpassword.text & "'",conn
    if rs.eof then
        msgbox "密码错误!"
    else
        msgbox "密码正确!"
    end if
      

  4.   

    上面的代码有安全性问题啊
    可以在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
      

  5.   

    还要考虑用户不合法的情况: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
      

  6.   

    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