• 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;User ID=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("username不能为空!")
                Return
            End If
    
            If TxtPassword.Text = "" Then
                MsgBox("password不能为空!")
                Return
            End If
    
            Try                                    'users接收实体
                users = mgr.UserLogin(user)
    
                If (users.UserID Is Nothing And users.Password Is Nothing) Then      '假设users不为空。则登陆成功!
                    MsgBox("登陆失败,username和password不匹配")
                    Return
                Else
                    MsgBox("登陆成功。登陆用户:" + user.UserID)
                End If
            Catch ex As Exception
                MsgBox(ex.Message())
            End Try
        End Sub
    End Class


  • 相关阅读:
    Java异常处理中,try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
    是否可以继承String类
    启动一个线程是用run()还是start()?
    关键字final 分别修饰一个类,一个方法,一个变量,各起什么作用
    Switch选择语句能否作用在String【字符串】上,也就是能否这么写:Switch(一个字符串变量)?
    char类型能否存储一个中文字符?为什么
    在java中,List是个接口,那实现List接口的类有哪些,有什么区别?
    请写出一个单例模式。
    数组有没有length()这个方法?String有没有length()这个方法?
    请写出5种常见到的runtime exception。
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7354897.html
Copyright © 2020-2023  润新知