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

📄 wblusers.vb

📁 数据库学习的绝好例子简单的数据库经典入门
💻 VB
字号:
Imports System.Text
Imports System.Security.Cryptography

Public Class WBLUsers
    Implements IDisposable

    'Private variables and objects
    Private objWDAUsers As WroxDataAccess.WDAUsers

    Private disposed As Boolean = False#Region " Constructor and Destructor "    Public Sub New(ByVal Company As String, ByVal Application As String)
        objWDAUsers = New WroxDataAccess.WDAUsers(Company, Application)
    End Sub

    ' IDisposable    Private Overloads Sub Dispose(ByVal disposing As Boolean)        If Not Me.disposed Then            If disposing Then                ' TODO: put code to dispose managed resources
            End If            objWDAUsers.Dispose()
            objWDAUsers = Nothing
        End If        Me.disposed = True    End Sub#End Region#Region " IDisposable Support "    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Overloads Sub Dispose() Implements IDisposable.Dispose        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)        GC.SuppressFinalize(Me)    End Sub    Protected Overrides Sub Finalize()        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(False)        MyBase.Finalize()    End Sub#End Region
#Region " Public User Functions "
    Public Function GetNewUserDS() As DataSet
        Try
            'Instantiate a new DataSet object
            GetNewUserDS = New DataSet

            'Create a DataTable object
            Dim objDataTable As DataTable = GetNewUserDS.Tables.Add("User")

            'Create a DataColumn object
            Dim objDataColumn As DataColumn

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("UserID", _
                Type.GetType("System.Guid"))
            objDataColumn.AllowDBNull = False

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("LoginName", _
                Type.GetType("System.String"))
            objDataColumn.AllowDBNull = False
            objDataColumn.MaxLength = 15

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("Password", _
                Type.GetType("System.String"))
            objDataColumn.AllowDBNull = True
            objDataColumn.MaxLength = 30

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("FirstName", _
                Type.GetType("System.String"))
            objDataColumn.AllowDBNull = False
            objDataColumn.MaxLength = 30

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("LastName", _
                Type.GetType("System.String"))
            objDataColumn.AllowDBNull = False
            objDataColumn.MaxLength = 30

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("Email", _
                Type.GetType("System.String"))
            objDataColumn.AllowDBNull = False
            objDataColumn.MaxLength = 50

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("Phone", _
                Type.GetType("System.String"))
            objDataColumn.AllowDBNull = False
            objDataColumn.MaxLength = 20

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("Status", _
                Type.GetType("System.Boolean"))
            objDataColumn.AllowDBNull = False

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("GroupID", _
                Type.GetType("System.Guid"))
            objDataColumn.AllowDBNull = False

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("RoleID", _
                Type.GetType("System.Guid"))
            objDataColumn.AllowDBNull = False

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("ManagerID", _
                Type.GetType("System.Guid"))
            objDataColumn.AllowDBNull = True

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

    Public Function AddUser(ByVal User As DataSet) As Boolean
        Try
            'Validate user data
            ValidateUserData(User)

            'Call the data component to add the new user
            Return objWDAUsers.AddUser(User)
        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 + -