三层架构实现登录——VB.NET版

          关于三层登录的具体描述和介绍请看我的上一篇博客,三层登录的实现。这篇博客的主要内容是使用VB.NET语言编写的三层登录功能,总的来说是大同小异。所以就将代码贴上来,仅供参考。

        实体层Model

</pre><pre name="code" class="vb">Public Class UserInfo

    Private _id As Integer
    Private _userName As String
    Private _passWord As String

    Public Property ID() As Integer          '定义字段的属性
        Get
            Return _id
        End Get
        Set(value As Integer)
            _id = value
        End Set
    End Property

    Public Property UserName() As String
        Get
            Return _userName
        End Get
        Set(value As String)
            _userName = value
        End Set
    End Property

    Public Property PassWord() As String
        Get
            Return _passWord
        End Get
        Set(value As String)
            _passWord = value
        End Set
    End Property

End Class


U层

Public Class Form

    Dim userName As String
    Dim passWord As String
    Dim mgr As BLL.LoginManager

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        userName = txtUserName.Text.Trim()
        passWord = txtPassWord.Text.Trim()          '获取Text控件中输入的内容

        mgr = New Login.BLL.LoginManager()          '实例化B层的对象mgr


        Try
            Dim user As New Model.UserInfo       '调用B层,传递U层数据,进行登录判断
            user = mgr.UserLogin(user)

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try

    End Sub
End Class


B层

Public Class LoginManager
    Public Function UserLogin(ByVal User As Model.UserInfo) As Model.UserInfo
        Dim uDao As New DAL.UserDAO              '实例化D层中新的UserDAO对象
        Dim user1 As Model.UserInfo              '定义一个类型为实体层的参数,用于赋值

        user1 = uDao.SelectUser(User)

        If IsNothing(user1.UserName) Then                         '判断是否查询到记录,若有,登录成功,并返回实体user1
            Throw New Exception("登录失败,请检查用户名和密码!")
        Else
            MsgBox("登录成功,马上进入系统")
            Return user1
        End If

    End Function
End Class



D层

Imports System.Data
Imports System.Data.SqlClient
Imports Login.Model             '引用命名空间
'D层,只涉及到对数据的操作,需要引用实体层Entity
Public Class UserDAO

  Public Function SelectUser(ByVal User As UserInfo) As Model.UserInfo   '传实体UserInfo,而不是参数Username和passWord等。    
        Dim conn As New SqlConnection(DbUtil.ConnString)
        Dim reader As SqlDataReader
        Dim eUser As New Model.UserInfo
        Dim sql As String = "Select UserName,PassWord FROM Users WHERE UserName=@userName AND PassWord=@passWord"   '执行查询语句,并生成一个DataReader
        Dim cmd As New SqlCommand(sql, conn)
        cmd.CommandText = sql
        cmd.CommandType = CommandType.Text
        cmd.Parameters.Add(New SqlParameter("@userName", User.UserName))
        cmd.Parameters.Add(New SqlParameter("@passWord", User.PassWord))
        conn.Open()
        reader = cmd.ExecuteReader()             '读取查询到的数据,并返回给相应的属性。

        While (reader.Read())
            '获取数据库中相应字段的数据
            '数组必须从0开始读取,否则会超出其界限
            eUser.UserName = reader.GetString(0)
            eUser.PassWord = reader.GetString(1)
        End While
        Return eUser
        conn.Close()

    End Function

Module DbUtil

    Public ConnString As String = "server=DESKTOP-F8291CO;database=Login;uid=sa;pwd=123456"    ' 连接数据库

End Module



总结:VB.NET的学习让我看到了VB升级版的样子,将VB真正的升级为一种面向对象的语言,确实非常强大,在个人机房重构的时候,要不断总结VB.NET的使用方法和原则。同时结合软件工程和UML将自己的整体知识都总结梳理一遍,相信会有很大的收获。




你可能感兴趣的:(三层架构实现登录——VB.NET版)