在系统登陆时如何将输入的用户名和密码与access中的数据对比,返回是否存在

解决方案 »

  1.   

    '******************************************以下是用户登陆操作****************************************
    '函数:用于系统用户登陆(密码登陆)
    '输入:(用户名)(密码)
    '输出:
    '返回:(1-登陆成功)(2-没有此用户)(3-密码错误)(else-登陆错误)
    Public Function LoginSys1&(ByVal strUser$, ByVal strPass$)
    Dim arrList()
    Dim sDBO As New clsDatabase
    Dim lngReturn&
    Dim lngCount&    lngReturn = sDBO.LinkMainDatabase() 'link database
        If lngReturn <> 0 Then
            lngReturn = sDBO.ExecuteTable(arrList(), "SELECT * " & _
                                                     "FROM SystemUser " & _
                                                     "WHERE UserName='" & strUser & "'", lngCount)
            
            If lngCount = 0 Then
                LoginSys1 = 2
                GoTo ExitFun
            Else
                If arrList(3, 0) <> strPass Then
                    LoginSys1 = 3
                    GoTo ExitFun
                Else
                    '记录登陆时间
                    sDBO.ExecuteCommand "UPDATE SystemUser " & _
                                        "SET LoginTime=#" & Now & "# " & _
                                        "WHERE UserName='" & strUser & "'"
                    LoginSys1 = 1
                End If
            End If
        Else
            '数据库打开失败
        End If
    ExitFun:
        sDBO.CloseConnection
        Set sDBO = Nothing
    End Function'函数:连接、查询表函数(返回记录集)
    '输入:
    '输出:记录条数
    '返回:-1 表示执行失败 N 表示错误号
    Public Function ExecuteTable&(arrList(), ByVal strSQLText$, lngCount&)
    On Error GoTo TableErr
    Dim sRs As New ADODB.Recordset    With sRs
            'sComm.CommandTimeout = 20
            sComm.CommandType = adCmdText
            sComm.CommandText = strSQLText
            Set sRs = sComm.Execute()
            
            '判断执行是否成功
            If Not sRs.EOF Then
                lngCount = sRs.RecordCount
                arrList = sRs.GetRows()
                ExecuteTable = 1
            Else
                lngCount = 0
                ExecuteTable = 1
            End If
            
            sRs.Close
            Set sRs = Nothing
        End With
        Exit Function
    TableErr:
        ExecuteTable = Err.Number
    End Function
      

  2.   

    Private Sub TestPwd()
            Dim cn As New ADODB.Connection
            Dim rs As New ADODB.Recordset
            cn.Open "provider=microsoft.jet.oledb.4.0;data source=e:\test1.mdb"
            rs.Open "select password from userinfo where username='" & txtusername.Text & "'"
            If rs.EOF Then
                MsgBox "不存在此用户", vbCritical
                txtusername.SetFocus
                Exit Sub
            Else
                If rs!Password <> txtPwd.Text Then
                    MsgBox "密码错误,重新输入密码。。", vbCritical
                    txtPwd.Text.SetFocus
                    Exit Sub
                Else
                    MsgBox "密码正确", vbInformation
                    '干别的....
                End If
            End If
            rs.Close
            Set rs = Nothing
            cn.Close
            Set cn = Nothing
        Exit Sub