[置顶] 【VB.NET】三层登陆实例

上一篇博客讲了三层的理论知识,光有理论没有实践是行不通的,接下来说一下VB.NET的实战演练。

(1)绘制包图

  [置顶] 【VB.NET】三层登陆实例_第1张图片

(2)设计数据库

 [置顶] 【VB.NET】三层登陆实例_第2张图片

 (3)新建项目

   [置顶] 【VB.NET】三层登陆实例_第3张图片

 (4)界面设计

  [置顶] 【VB.NET】三层登陆实例_第4张图片

(5)添加引用

  [置顶] 【VB.NET】三层登陆实例_第5张图片

(5)代码实现

 Entity层

  

Public Class UserInfo
    '定义两个私有属性
    Private UserID As String
    Private Password As String

    '定义属性过程,通过属性过程,允许他的类访问该属性
    Public Property ID As String
        Get
            Return UserID
        End Get
        Set(ByVal value As String)
            UserID = value
        End Set
    End Property
    Public Property PWD As String
        Get
            Return Password
        End Get
        Set(ByVal value As String)
            Password = value
        End Set
    End Property
End Class

DAL层

Imports System.Data.SqlClient
Imports LoginEntity


Public Class UserDAO
    Dim sqlConnectStr As String = "server=lixiaodong;database=Login;uid=sa;pwd=123456;"
    Dim sqlConnection1 As SqlConnection = New SqlConnection(sqlConnectStr)

    Function selectUserInfoFormUserInfoTabel(ByVal user As LoginEntity.UserInfo) As LoginEntity.UserInfo
        'Dim sql As String = "select * from Users Where Username='" & user.ID & "'"
        Dim sql As String = "select * from Users where UserName='" & user.ID & "'"
        Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1) '定义sqlcommand对象

        Dim read As SqlDataReader   '定义sqlDataReader对象

        Dim UserDataTable As New DataTable '定义一个DataTable对象
        Dim user1 As New LoginEntity.UserInfo

        Try
            sqlConnection1.Open()
            read = cmd.ExecuteReader()
            UserDataTable.Load(read)
            user1.ID = UserDataTable.Rows(0)("Username")
            user1.PWD = UserDataTable.Rows(0)("Password")
            Return user1
        Catch ex As Exception
            user1.PWD = ""
            Return user1
        Finally
            If Not IsNothing(sqlConnection1) Then
                sqlConnection1.Close()
            End If
        End Try


    End Function

End Class

BLL层

Imports LoginEntity
Imports LoginDAL.UserDAO
'B层业务处理逻辑
Public Class LoginMananger
    Function SelectUIandEntity(ByVal user As LoginEntity.UserInfo) As Boolean
        Dim DalUser As New LoginDAL.UserDAO
        Dim EntityUser As New LoginEntity.UserInfo

        EntityUser.ID = user.ID
        EntityUser = DalUser.selectUserInfoFormUserInfoTabel(EntityUser)

        If EntityUser.PWD = user.PWD Then
            Return True
        Else
            Return False
        End If
    End Function
End Class

UI层

Public Class UserLogin

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click

        '实例化传实体的对象
        Dim loginUser As New LoginEntity.UserInfo
        '实例化BLL层的对象
        Dim Bcheck As New LoginBLL.LoginMananger

        '将文本框中的字符串赋给实体对象login.User 使实体对象能够带上参数
        loginUser.ID = txtUserName.Text()
        loginUser.PWD = txtPassword.Text()


        If Bcheck.SelectUIandEntity(loginUser) Then
            MsgBox("登陆成功!")
        Else
            MsgBox("登陆失败!")
        End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        End
    End Sub
End Class

小结:

        三层的实例到此结束,通过敲三层让我更加了解是如何分层实现各自的任务,又是如何的解耦,敲三层的过程中通过查资料学到了很多与VB不同的地方,革命尚未成功,同志仍需努力!!


   


你可能感兴趣的:([置顶] 【VB.NET】三层登陆实例)