VB.NET版+三层实现登陆

前端之家收集整理的这篇文章主要介绍了VB.NET版+三层实现登陆前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

三层已经学了一些时间了,开始觉得自己可以用C#写程序了,就用C#写了一个实现登陆的,真正再用在机房中,还是觉得很吃力的,所以,决定用vb.net敲了。下面是我用vb.net实现的登陆,可以给大家做一些参考。

这是构架的三层

数据库中的表T_User_DAL:UserI,Level,Password,Head,computer

首先看一下

Entity层:

Public Class UserInfoEntity
#Region "定义变量"
    Private _userID As String
    Private _level As String
    Private _Head As String
    Private _password As String
#End Region
    Public Property UserID() As String            '将T_User_DAL表中的每一个实体都传上去,实体是根据表来建的,不是功能
        Get
            Return _userID
        End Get
        Set(value As String)
            _userID = value
        End Set
    End Property
    Public Property Level() As String
        Get
            Return _level
        End Get
        Set(ByVal value As String)
            _level = value
        End Set
    End Property
    Public Property Head() As String
        Get
            Return _Head
        End Get
        Set(value As String)
            _Head = 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


DAL层:

为了能够降低耦合一定要将连接字符串拿出来,不要每一个DAL中都写一边。

Imports System.Data
Imports System.Data.sqlClient
Public Class sqlUtil
    Public Shared Function connstring() As String
        'connstring = "Server=192.168.24.183;Database=ReconsitutionCharge_sys;UserID=sa;Password=123456"     ‘会出现无法识别userid的错误
        connstring = "Server=mx; Database=ReconsitutionCharge_sys; User ID=sa; Password=123456"         '连接sql的字符串
    End Function

End Class






T_User_dal表DAL层代码

Imports System.Data.sqlClient
Imports System.Data
Public Class UserDAL
    Public Function SelectUser(ByVal user As Entity.UserInfoEntity) As Entity.UserInfoEntity
        Dim conn As New sqlConnection                          '定义连接打开数据库
        Dim cmd As New sqlCommand                              '定义数据库命令
        conn = New sqlConnection(sqlUtil.connstring())         '实例化sqlUntil中返回的字符串 
        cmd.Connection = conn


        cmd.CommandText = "Select * From T_User_DAL Where UserID=@UserID and Password=@Password"        '存储过程


        cmd.Parameters.Add(New sqlParameter("@UserID",user.UserID))                        '赋值,传参
        cmd.Parameters.Add(New sqlParameter("@Password",user.Password))
        cmd.CommandType = CommandType.Text                                                  'CommandText 属性设置为存储过程的名称。当调用 Execute 方法之一时,该命令将执行此存储过程
        conn.Open()                                                                         '打开连接




        Dim reader As sqlClient.sqlDataReader                                               '读取数据库中的表
        reader = cmd.ExecuteReader()


        Dim users As New Entity.UserInfoEntity                                              '实例化实体


        While (reader.Read())
            'If users Is Nothing Then
            '    user = New Entity.UserInfoEntity
            'End If
            users.UserID = reader.GetString(reader.GetOrdinal("UserID"))
            users.Password = reader.GetString(reader.GetOrdinal("Password"))
        End While
        conn.Close()
        Return users
    End Function
End Class



BLL层:

Public Class LoginBLL
    Function UserLogin(ByVal user As Entity.UserInfoEntity) As Entity.UserInfoEntity
        Dim uDAL As New DAL.UserDAL                                 '实例化了DAL中的UserDAL
        Dim users As New Entity.UserInfoEntity                      '实例化了Entity层信息表

        users = uDAL.SelectUser(user)                               'selectUser是返回一个Entity类的,给它赋值user
        Return users                                                '返回users实体
    End Function

End Class



UI层:

界面设计:



Private Sub btnLogin_Click(sender As Object,e As EventArgs) Handles btnLogin.Click
        Dim mgr As New BLL.LoginBLL             '实例业务层
        Dim user As New Entity.UserInfoEntity   '实例实体
        Dim users As Entity.UserInfoEntity      '接收实体,用于接收UserLogin()返回的数据类型,如果没有New就不是实例化


        user.UserID = txtUserID.Text.Trim()     '给实体层传入值
        user.Password = TxtPassword.Text.Trim()
        If txtUserID.Text = "" Then             '判断输入是否正确
            MsgBox("用户名不能为空!")
            Return
        End If

        If TxtPassword.Text = "" Then
            MsgBox("密码不能为空!")
            Return
        End If

        Try                                    'users接收实体
            users = mgr.UserLogin(user)

            If (users.UserID Is Nothing And users.Password Is Nothing) Then      '如果users不为空,则登陆成功!
                MsgBox("登陆失败,用户名和密码不匹配")
                Return
            Else
                MsgBox("登陆成功!登陆用户:" + user.UserID)
            End If
        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub
End Class
原文链接:https://www.f2er.com/vb/258050.html

猜你在找的VB相关文章