七层登录实现

              之前只敲了三层的登录窗体,过了很久才把七层的补上。首先看一张图:

            

             感觉这张图就是精髓,按照以上的这种关系,七层逐渐完善。代码如下:

Facade层:

Imports LoginBLL
Imports LoginEntity
Public Class LoginFacade
    Public Function CheckUser(ByVal UserInfo As LoginEntity.LoginUserEntity) As Boolean
        '用于检查用户是否存在    
        Dim IsUserExists As New LoginBLL.LoginBLL()
        Dim flag As Boolean
        flag = IsUserExists.IsUserExists(UserInfo)
        If flag = True Then
            Return True
        Else
            Return False
        End If
    End Function
    '检查密码是否正确    
    Public Function CheckPwd(ByVal UserInfo As LoginEntity.LoginUserEntity) As DataTable
        Dim IsPwd As New LoginBLL.LoginBLL()
        Dim table As DataTable
        table = IsPwd.isPWDright(UserInfo)
        Return table
    End Function

End Class
Factory层:

Imports System.Configuration    '添加对配置文件的引用    
Imports System.Reflection       '添加对反射的引用    
Imports IDAL
Public Class LoginFactory
    '读配置文件,D层的每个类都在配置文件里对应一个KEY  
    '下面这句是把key设成变量,然后再下面的方法中只用这个变量就可以应用D层的这个类了。  
    Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("strDB")
    Public Function CreateUserInfo() As IuserinfoDAL
        'CType是一个内联函数,将前部分的表达式转换为后面的类型  
        Return CType(Assembly.Load("LoginDAL").CreateInstance("LoginDAL" & "." & "LoginUserDAL"), IuserinfoDAL)     '返回IuserinfoDAL  
    End Function

    Public Function CreateScoreInfo() As IDAL.IScore
        'CType是一个内联函数,将前部分的表达式转换为后面的类型  
        Return CType(Assembly.Load("LoginDAL").CreateInstance("LoginDAL" & "." & "ScoreDAL"), LoginDAL.ScoreDAL)     '返回IuserinfoDAL  
    End Function
End Class
IDAL层:

Public Interface IScore
    Function UpdateScore(ByVal score As LoginEntity.ScoreEntity) As Boolean
End Interface

Imports LoginEntity
Public Interface IuserinfoDAL
    'UserInfo为用户信息的实体,是由实体类实例化来的。  
    '即所谓的传实体。  
    '此接口定义了一个方法,用以检查用户是否存在  
    Function selectUser(ByVal UserInfo As LoginUserEntity) As DataTable

End Interface
LoginBLL层:

Imports Factory
Imports IDAL
Imports LoginEntity
Public Class LoginBLL
    '检查用户是否存在  
    Public Function IsUserExists(ByVal UserInfo As LoginEntity.LoginUserEntity) As Boolean
        Dim factory As New Factory.LoginFactory
        'Dim Iuser As IDAL.IuserinfoDAL
        Dim Iuser As IDAL.IuserinfoDAL
        '调用"创建用户"的工厂方法  
        'Iuser = factory.CreateUserInfo()
        Iuser = factory.CreateUserInfo
        Dim table As DataTable
        ' Dim flag As Boolean
        Dim flag As Boolean
        table = Iuser.selectUser(UserInfo)
        '由于在sqlHelper中返回的形式为表格形式(adataset.Tables(0)),且开头第一列表示为0,所以Item(0)则代表用户名  
        If table.Rows(0).Item(1) = "" Then
            flag = False
        Else
            flag = True
        End If
        Return flag
    End Function
    '查看密码是否正确  
    Public Function isPWDright(ByVal UserInfo As LoginEntity.LoginUserEntity) As DataTable
        Dim factory As New Factory.LoginFactory()
        Dim Iuser As IDAL.IuserinfoDAL
        Dim table As DataTable   '中间变量,用于存储D层查询到的数据    
        Iuser = factory.CreateUserInfo   '调用工厂的CreateUserInfo方法创建Iuser接口实例    
        table = Iuser.selectUser(UserInfo)  '调用接口的方法selectUser.    
        Return table
    End Function

    Public Function AddScore(score As LoginEntity.ScoreEntity) As Boolean
        Dim factory As New Factory.LoginFactory()
        Dim IScore As IDAL.IScore
        IScore = factory.CreateScoreInfo()
        Return IScore.UpdateScore(score)
    End Function
End Class
LoginDAL层:

Imports System.Data.SqlClient  ' 命名空间是 SQL Server 的 .NET Framework 数据提供程序。  
'SQL Server 的 .NET Framework 数据提供程序描述了一个类集合,这个类集合用于访问托管空间中的 SQL Server 数据库。  
Imports LoginEntity
Imports IDAL
Public Class LoginUserDAL : Implements IDAL.IuserinfoDAL '实现接口中的方法。  
    '声明并实例化SQLHelper类  
    'Private sqlHelper As SQLHelper.sqlHelper = New SQLHelper.sqlHelper()


    Public Function selectUser(UserInfo As LoginUserEntity) As DataTable Implements IuserinfoDAL.selectUser
        Dim sqlHelper As New SQLHelper.sqlHelper()
        Dim Sql As String
        Dim table As DataTable '中间变量,用于储存从数据库中查找到的信息                                                         '声明一个DataTable类型变量    
        Dim sqlParams As SqlParameter() = {New SqlParameter("@UserName", UserInfo.userName), New SqlParameter("@password", UserInfo.password)} '声明并实例化参数数组   
        Sql = "select * from Users where userName=@UserName and password=@password"
        '下句为调用SqlHelper类中的GetDataTable()方法来执行查询,并获取返回值  
        table = sqlHelper.GetDataTable(Sql, CommandType.Text, sqlParams)
        Return table
    End Function
   
End Class

Imports System.Data.SqlClient
Imports LoginEntity
Public Class ScoreDAL : Implements IDAL.IScore
    Public Function UpdateScore(ByVal score As ScoreEntity) As Boolean Implements IDAL.IScore.UpdateScore
        Dim conn As SqlConnection = New SqlConnection("server=.;database=Login;uid=sa;pwd=123")

        Dim cmd As SqlCommand = conn.CreateCommand()
        cmd.CommandText = "INSERT INTO SCORES(UserName,Score) Values(@UserName,@Score)"
        cmd.Parameters.Add(New SqlParameter("@UserName", score.UserName))
        cmd.Parameters.Add(New SqlParameter("@Score", score.Score))
        conn.Open()
        cmd.ExecuteNonQuery()
        Return True
    End Function
End Class
LoginEntity层:

Public Class LoginUserEntity
    Private strUserName As String  '实例化得到的用户名和密码  
    Private strPassword As String
    Public Property userName() As String  '设置userName的属性  
        Get
            Return strUserName
        End Get
        Set(value As String)
            strUserName = value
        End Set
    End Property
    Public Property password() As String
        Get
            Return strPassword
        End Get
        Set(value As String)
            strPassword = value
        End Set
    End Property
End Class

Public Class ScoreEntity
    Private strUserName As String  '实例化得到的用户名和密码  
    Private strScore As Integer
    Public Property UserName() As String  '设置userName的属性  
        Get
            Return strUserName
        End Get
        Set(value As String)
            strUserName = value
        End Set
    End Property
    Public Property Score() As Integer
        Get
            Return strScore

        End Get
        Set(value As Integer)
            strScore = value
        End Set
    End Property
End Class
LoginUI层:

Imports LoginBLL
Imports LoginEntity
Imports Facade
Public Class Form1
    Dim ScoreBLL As New LoginBLL.LoginBLL()
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtBox1.Text = "" Then
            MessageBox.Show("请输入用户名!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtBox1.Text = ""
            txtBox1.Focus()
            Exit Sub
        ElseIf txtBox2.Text = "" Then
            MessageBox.Show("请输入密码!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtBox2.Text = ""
            txtBox2.Focus()
            Exit Sub
        End If
        Dim ScoreEntity As New LoginEntity.ScoreEntity()
        ScoreEntity.Score = 10
        ScoreEntity.UserName = txtBox1.Text
        Try
            '定义一个外观层的对象    
            Dim FacadeLogin As New Facade.LoginFacade
            Dim UserInfo As New LoginEntity.LoginUserEntity()
            UserInfo.userName = Trim(txtBox1.Text)
            UserInfo.password = Trim(txtBox2.Text)
            Dim strResult1 As Boolean
            strResult1 = FacadeLogin.CheckUser(UserInfo) '将U层的文本框的内容传入外观层,然后通过外观层传入B层进行判断  
            If strResult1 = False Then
                MsgBox("用户不存在!")
                txtBox1.Text = ""
                txtBox2.Text = ""
                txtBox1.Select()
                txtBox1.Focus()
            End If
            Dim table As DataTable
            table = FacadeLogin.CheckPwd(UserInfo)
            If Trim(txtBox2.Text) = Trim(table.Rows(0).Item(2)) Then
                ScoreBLL.AddScore(ScoreEntity)
                MsgBox("登陆成功!")
                txtBox1.Text = ""
                txtBox2.Text = ""

            End If
        Catch ex As Exception
            MsgBox("用户不存在或者密码不正确")
            txtBox1.Text = ""
            txtBox2.Text = ""
            txtBox1.Select()
            txtBox1.Focus()
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtBox1.Select()
        txtBox1.Focus()
    End Sub
End Class

SQLHelp层:

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data
Imports System.Reflection

Public Class sqlHelper
    Public Shared ConnectionString As String = ConfigurationManager.AppSettings("ConnString")
    ''' <summary>  
    ''' 执行带参数的查询操作  
    ''' </summary>  
    ''' <param name="cmdTxt">参数cmdTxt为所要执行的sql语句</param>  
    ''' <param name="cmdType">查询时的查询方式</param>  
    ''' <param name="paras">查询时的命令参数paras</param>  
    ''' <returns>查询后以表的方式返回,如下面的adataset.Tables(0)</returns>  
    ''' <remarks></remarks>  
    Public Shared Function GetDataTable(ByVal cmdTxt As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter()) As DataTable
        Dim conn As SqlConnection = New SqlConnection(ConnectionString) '建立数据库连接 
        Dim cmd As SqlCommand '定义命令变量cmd  
        Dim adaptor As SqlDataAdapter '定义数据适配器  
        Dim adataset As DataSet '定义并实例化数据缓冲区对象,即从数据库传入的对象。  
        cmd = New SqlCommand(cmdTxt, conn) '在conn上面执行实例化命令变量,并执行语句cmdType  
        cmd.CommandType = cmdType '命令执行的类型  
        cmd.Parameters.AddRange(paras) '命令执行时的参数  
        adaptor = New SqlDataAdapter(cmd) '将结果绑定到数据适配器变量adaptor上面  
        adataset = New DataSet
        Try
            '如果数据库连接状态为关闭则将其打开  
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
            adaptor.Fill(adataset) '向adaptor对象中填充查询的数据  
        Catch ex As Exception
            '错误处理程序,出错则提示  
            MsgBox(ex.Message, , "数据库操作")
        Finally
            '如果连接状态为打开则将其关闭,释放内存  
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try
        '以表格形式返回结果  
        Return adataset.Tables(0)
    End Function
End Class

            以上是所有代码,只有自己真正敲完,才能体会到七层的奥妙!

 







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 41
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 41
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值