vb.net版三层登录

vb.net的语言和VB很相像,但和C#还是有点差别的。这段时间一直看C#的东西,刚换成vb.net还有点不适应。

登录逻辑

1.创建实体类
2.UI实例化一个实体,并把界面数据创给实体,调用BLL的UserLogin方法。
3.实例化DAL层的DAO类,调用D层的SelectUser方法并判断用户是否存在
4.创建打开数据连接;把user清空,如果数据库中有要找的数据就把这些数据赋给user

登录实例

三层及实体

vb.net版三层登录_第1张图片

U层

Public Class LoginUI
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        Try
            '获得介面层数据
            Dim user As New Entity.UserInfo

            user.ID = txtUserID.Text.Trim()
            user.PSW = txtPassWord.Text.Trim()

            '调用B层,登录判断
            Dim svc As New LoginBLL.LoginService
            user = svc.UserLogin(user)

            '显示登录成功
            MsgBox("用户" + "'" + user.UserName + "'" + "登录成功")

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

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

BLL层

'B层,主要涉及业务逻辑
Public Class LoginService
    Public Function UserLogin(ByVal user As Entity.UserInfo) As Entity.UserInfo
        Dim uDao As New LoginDAL.UserDAO

        user = uDao.SelectUser(user)

        '判断是否有记录,有记录返回user,不成功给出提示
        If IsNothing(user) Then
            Throw New Exception("登录失败,请检查用户ID和密码") '这里抛出错误,在UI层Catch
        Else

            Return user
        End If
    End Function
End Class

D层

'引用空间命名
Imports System.Data
Imports System.Data.SqlClient

'D层,只涉及到对数据的操作
Public Class UserDAO
    '创建数据库连接
    Public conn As New SqlConnection("server= jf; database = Login; user=sa;password= summer")
    Public Function SelectUser(ByVal user As Entity.UserInfo) As Entity.UserInfo
        Dim reader As SqlDataReader
        '其实下面的这段程序就相当于dim sql as string ="select ID,UserName,PWD From Users Where UserName='"User.UserName"' And PWD='"User.PWD"'"
        Dim sql As String = "Select ID,PassWord,UserName From Users Where ID = @ID And PassWord = @PassWord"
        Dim cmd As New SqlCommand(sql, conn)   '创建一个sql命令,第一个参数是CommandText,第二个是sqlConnection
        cmd.CommandText = sql
        cmd.CommandType = CommandType.Text
        cmd.Parameters.Add(New SqlParameter("@ID", user.ID))
        cmd.Parameters.Add(New SqlParameter("@PassWord", user.PSW))

        conn.Open()   '打开数据连接
        reader = cmd.ExecuteReader()  '执行查询命令操作

        user = Nothing
        '读取查询到的数据,并返回给相应的属性
        While reader.Read()
            If user Is Nothing Then  '如果没有找到对应的数据就把user设为空,在UI层判断如果user是空就报错
                user = New Entity.UserInfo
            End If
            '获取数据库相应字段的数据
            user.ID = reader.GetInt32(0)
            user.PSW = reader.GetString(2)
            user.UserName = reader.GetString(1)


        End While
        Return user
        conn.Close()

    End Function
End Class

Entity实体

Public Class UserInfo
    'ID属性
    '不用括号,和分号
    Private _id As Integer
    Public Property ID As Integer
        Get
            Return _id
        End Get
        Set(value As Integer)
            _id = value
        End Set
    End Property

    '用户名
    Private _username As String
    Public Property UserName As String
        Get
            Return _username
        End Get
        Set(value As String)
            _username = value
        End Set
    End Property

    '密码
    Private _psw As String
    Public Property PSW As String
        Get
            Return _psw
        End Get
        Set(value As String)
            _psw = value
        End Set
    End Property




End Class

总结

C#版的登录时看着视频敲的,敲完后感觉糊里糊涂,知道有三层,具体三层怎么运作有些疑问。Vb.net版是我看了一遍师哥的成果后,自己敲的,有的地方,像user的处理和他有点不一样,他每一层都定义了一个user,user1,user2, user3 是调用时为了区分是哪个层的user。我感觉没有必要区分,因为不管UI层是叫user1还是user2,到BLL层传进去都叫user,返回的也是user。做了一个大胆的尝试,不过还是运行成功了,好开森。

还有一个尝试是模仿C#版的,在D层写了一个DBConnection的类,把连接数据库的代码写 进去,想着如果以后改数据库,就该这个类就好了,不用修改UserDAO类,也算是解耦了,但是在UserDAOI类中总是做不好引用,只能暂时放弃了。Vb.net和C#还是有些不同的,有待继续研究。

你可能感兴趣的:(VB.NET)