⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wblusers.vb

📁 数据库学习的绝好例子简单的数据库经典入门
💻 VB
📖 第 1 页 / 共 2 页
字号:
    Public Function ValidateLogin(ByVal LoginName As String, _
        ByVal Password As String) As DataSet

        Try
            'Call the data component to get a specific user
            ValidateLogin = objWDAUsers.ValidateLogin(LoginName, _
                HashPassword(Password))
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

    Public Function UpdateUser(ByVal User As DataSet) As Boolean
        Try
            'Validate the Login Name exists
            If User.Tables("User").Rows(0).Item( _
                "LoginName").ToString.Trim.Length = 0 Then
                Throw New System.Exception("Login Name is a required field.")
            End If

            'Validate the First Name exists
            If User.Tables("User").Rows(0).Item( _
                "FirstName").ToString.Trim.Length = 0 Then
                Throw New System.Exception("First Name is a required field.")
            End If

            'Validate the Last Name exists
            If User.Tables("User").Rows(0).Item( _
                "LastName").ToString.Trim.Length = 0 Then
                Throw New System.Exception("Last Name is a required field.")
            End If

            'Validate the password
            If User.Tables("User").Rows(0).Item( _
                "Password").ToString.Trim.Length = 0 Then
                'The old password is not being updated so set it to Null
                User.Tables("User").Rows(0).Item("Password") = DBNull.Value
            Else
                'A new password has been supplied
                If Not IsValidPassword(User.Tables( _
                    "User").Rows(0).Item("Password")) Then
                    Throw New System.Exception(strErrorMessage)
                Else
                    'Hash the password
                    User.Tables("User").Rows(0).Item("Password") = _
                        HashPassword(User.Tables("User").Rows(0).Item( _
                        "Password"))
                End If
            End If

            'Validate the email
            If Not IsValidEmail(User.Tables( _
                "User").Rows(0).Item("Email")) Then
                Throw New System.Exception(strErrorMessage)
            End If

            'Validate the phone number
            If Not IsValidPhoneNumber(User.Tables( _
                "User").Rows(0).Item("Phone")) Then
                Throw New System.Exception(strErrorMessage)
            End If

            'Validate manager
            If Not IsDBNull(User.Tables("User").Rows(0).Item("ManagerID")) Then
                If Not TypeOf User.Tables("User").Rows(0).Item("ManagerID") _
                    Is Guid Then
                    If User.Tables("User").Rows(0).Item("ManagerID") = _
                        String.Empty Then
                        'Set it to a null value
                        User.Tables("User").Rows(0).Item("ManagerID") = _
                            DBNull.Value
                    End If
                End If
            End If

            'Trim spaces
            User.Tables("User").Rows(0).Item("LoginName") = _
                User.Tables("User").Rows(0).Item("LoginName").ToString.Trim
            User.Tables("User").Rows(0).Item("Password") = _
                User.Tables("User").Rows(0).Item("Password").ToString.Trim
            User.Tables("User").Rows(0).Item("FirstName") = _
                User.Tables("User").Rows(0).Item("FirstName").ToString.Trim
            User.Tables("User").Rows(0).Item("LastName") = _
                User.Tables("User").Rows(0).Item("LastName").ToString.Trim
            User.Tables("User").Rows(0).Item("Email") = _
                User.Tables("User").Rows(0).Item("Email").ToString.Trim

            'Format the phone number
            User.Tables("User").Rows(0).Item("Phone") = _
                FormatPhoneNumber(User.Tables("User").Rows(0).Item("Phone"))

            'Call the data component to update the user
            Return objWDAUsers.UpdateUser(User)
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
             ExceptionErr.InnerException)
        End Try
    End Function

    Public Function DeleteUser(ByVal UserID As Guid) As Boolean
        Try
            'Call the data component to delete the user
            Return objWDAUsers.DeleteUser(UserID)
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function
#End Region

#Region " Private User Functions "
    Private Function HashPassword(ByVal strPassword As String) As String
        Try
            'Declare local variables
            Dim bytPasswordIn() As Byte
            Dim bytPasswordOut() As Byte

            Using objHashAlgorithm As New SHA1CryptoServiceProvider
                'Convert the input password to an array of bytes
                bytPasswordIn = Encoding.UTF8.GetBytes(strPassword)

                'Compute the Hash (returns an array of bytes)
                bytPasswordOut = objHashAlgorithm.ComputeHash(bytPasswordIn)

                'Return a base 64 encoded string of the hashed password
                HashPassword = Convert.ToBase64String(bytPasswordOut)
            End Using
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function
#End Region

#Region " Private Validation Functions "
    Private Sub ValidateUserData(ByRef User As DataSet)
        'Validate the Login Name exists
        If User.Tables("User").Rows(0).Item( _
            "LoginName").ToString.Trim.Length = 0 Then
            Throw New System.Exception("Login Name is a required field.")
        End If

        'Validate the First Name exists
        If User.Tables("User").Rows(0).Item( _
            "FirstName").ToString.Trim.Length = 0 Then
            Throw New System.Exception("First Name is a required field.")
        End If

        'Validate the Last Name exists
        If User.Tables("User").Rows(0).Item( _
            "LastName").ToString.Trim.Length = 0 Then
            Throw New System.Exception("Last Name is a required field.")
        End If

        'Validate the password
        If Not IsValidPassword(User.Tables( _
            "User").Rows(0).Item("Password")) Then
            Throw New System.Exception(strErrorMessage)
        End If

        'Validate the email address
        If Not IsValidEmail(User.Tables( _
            "User").Rows(0).Item("Email")) Then
            Throw New System.Exception(strErrorMessage)
        End If

        'Validate the phone number
        If Not IsValidPhoneNumber(User.Tables( _
            "User").Rows(0).Item("Phone")) Then
            Throw New System.Exception(strErrorMessage)
        End If

        'Validate manager
        If Not IsDBNull(User.Tables("User").Rows(0).Item("ManagerID")) Then
            If Not TypeOf User.Tables("User").Rows(0).Item("ManagerID") _
                Is Guid Then
                If User.Tables("User").Rows(0).Item("ManagerID") = _
                    String.Empty Then
                    'Set it to a null value
                    User.Tables("User").Rows(0).Item("ManagerID") = _
                        DBNull.Value
                End If
            End If
        End If

        'Trim spaces
        User.Tables("User").Rows(0).Item("LoginName") = _
            User.Tables("User").Rows(0).Item("LoginName").ToString.Trim
        User.Tables("User").Rows(0).Item("Password") = _
            User.Tables("User").Rows(0).Item("Password").ToString.Trim
        User.Tables("User").Rows(0).Item("FirstName") = _
            User.Tables("User").Rows(0).Item("FirstName").ToString.Trim
        User.Tables("User").Rows(0).Item("LastName") = _
            User.Tables("User").Rows(0).Item("LastName").ToString.Trim
        User.Tables("User").Rows(0).Item("Email") = _
            User.Tables("User").Rows(0).Item("Email").ToString.Trim

        'Hash the password
        User.Tables("User").Rows(0).Item("Password") = _
            HashPassword(User.Tables("User").Rows(0).Item("Password"))

        'Format the phone number
        User.Tables("User").Rows(0).Item("Phone") = _
            FormatPhoneNumber(User.Tables("User").Rows(0).Item("Phone"))
    End Sub
#End Region
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -