三层登录——VB.NET版

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zlt995768025/article/details/50578616

前言

    由于下面的机房收费系统重构自己要用VB.NET进行重构,所以在敲三层登录的时候,实践了一份C#版三层登录,接着就是VB.NET版的三层登录。话说还有七层登录,一下子感觉三层又矮小了。万丈高楼平地起,都得从最底层走起~

VB.NET版三层登录

Entity实体层

<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class UserInfo
    '定义两个私有属性
    Private UserID As String
    Private Password As String

    '定义属性过程,通过属性过程,允许其他类访问
    Public Property ID As String

        Get
            Return UserID
        End Get

        Set(value As String)
            UserID = value
        End Set
    End Property

    Public Property PWD As String

        Get
            Return Password
        End Get

        Set(value As String)
            Password = value
        End Set
    End Property
End Class</span>

U层

<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class FrmLogin

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        End
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        '实例化传实体对象;
        Dim LoginUser As New Login.Model.UserInfo '用来接收用户的登录名和密码;

        '实例化B层对象
        Dim Bcheck As New Login.BLL.LoginManager

        '将文本框中的字符串送给实体对象login.user使实体对象能够带上参数;
        LoginUser.ID = txtUsername.Text()
        LoginUser.PWD = txtPassword.Text()


        If Bcheck.selectUIandModel(LoginUser) Then
            MsgBox("恭喜你,成功了!Amazing")
        Else
            MsgBox("这也太笨了吧,连这个都做不好,怎么谈是精英?")
        End If
    End Sub
End Class</span>

B层

<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports Login.Model
Imports Login.DAL.UserDAO

Public Class LoginManager
    'B层业务处理逻辑:承接U层的参数和传递到D层;
    Function selectUIandModel(ByVal user As Login.Model.UserInfo) As Boolean

        Dim DAOuser As New Login.DAL.UserDAO '实例化D层的UserDAO类
        Dim Moderuser As New Login.Model.UserInfo '实例化实体层UserInfo类

        Moderuser.ID = user.ID
        Moderuser = DAOuser.selectUserInfoFormUserInfoTable(Moderuser) '引用了DAL层的方法

        If Moderuser.PWD = user.PWD Then
            Return True
        Else
            Return False
        End If
    End Function
End Class</span>

D层

<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports System.Data.SqlClient
Imports Login.Model

Public Class UserDAO
    'server是服务器的名称,可以是local,IP地址,计算机名称;
    'database是数据库的名称;
    'userid是登录的用户名;
    'pwd是登录的密码;
    Dim sqlConnectStr As String = "server =ZLT;database=Login;uid=sa;pwd=123"

    '下面可以用windows身份验证登录;
    'Dim sqlConnectStr1 As String = "server=ZLT;database=Login;integrated Security=true"

    Dim sqlConnect1 As SqlConnection = New SqlConnection(sqlConnectStr)

    Function selectUserInfoFormUserInfoTable(ByVal user As Login.Model.UserInfo) As Login.Model.UserInfo
        Dim sql As String = "select * from Users where Username='" & user.ID & "'"
        Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnect1) '定义sqlcommand对象;

        Dim read As SqlDataReader '定义sqldatareader对象;

        Dim UserDataTable As New DataTable '定义一个Datatable对象;
        Dim user1 As New Login.Model.UserInfo

        Try
            sqlConnect1.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(sqlConnect1) Then
                sqlConnect1.Close()
            End If
        End Try
    End Function
End Class</span>

小结

    1、人总得学会长大,三层也需要不断的创新;

    2、万变不离其宗,变是永远不变的。

点我——>C#版三层登录


感谢您的宝贵时间~~~

展开阅读全文

没有更多推荐了,返回首页