登录分层实例

6 篇文章 0 订阅

      初次写分层的程序,是从用户登录开始,尝试了不同的方法,首先是将许多数据库参数变量的都直接写到了数据访问层,这样对于程序修改及其不益,然后就封装到了模板层也就是实体层中,通过声明对应的常量实现,在真正的开发中有关数据访问的sql语句都是采用存储过程,然后又用存储过程的方法尝试了一下。分层有很大的好处,就不多说了,但是分层却很灵活,怎么寻找一个最优的策略,需要一个过程,这也是我们要成长的地方所在。

      来看一下代码吧,实体层就忽略了,主要是数据访问层、业务逻辑层和界面层。其中最重要的应该是在业务逻辑层。

      登录数据访问层:

''' <summary>
    ''' 获取用户ID
    ''' </summary>
    ''' <param name="modelUserInfo">实体层</param>
    ''' <returns>用户ID</returns>
    ''' <remarks></remarks>
    Public Function GetUserID(ByVal modelUserInfo As DataModel.m_UserInfo) As Object
        Dim sql As String = "UserInfo_GetUserID"
        Dim conn As New SqlConnection(connStr)
        Dim cmd As New SqlCommand(sql, conn)
        AddCmdParam(cmd, m_UserInfo.DBPARAM_USERID, modelUserInfo.userID)
        Try
            conn.Open()
            Return cmd.ExecuteScalar
        Catch ex As Exception
            Throw New System.Exception(ex.Message)
        Finally
            Close(conn)
            Close(cmd)
        End Try

    End Function
    ''' <summary>
    ''' 获取密码
    ''' </summary>
    ''' <param name="modelUserInfo"></param>
    ''' <returns>密码</returns>
    ''' <remarks></remarks>
    Public Function GetUserPwd(ByVal modelUserInfo As DataModel.m_UserInfo) As Object
        Dim sql As String = "UserInfo_GetUserPwd"
        Dim conn As New SqlConnection(connStr)
        Dim cmd As New SqlCommand(sql, conn)

        AddCmdParam(cmd, m_UserInfo.DBPARAM_USERID, modelUserInfo.userID)
        AddCmdParam(cmd, m_UserInfo.DBPARAM_USERPWD, modelUserInfo.userPwd)

        Try
            conn.Open()
            Return cmd.ExecuteScalar
        Catch ex As Exception
            Throw New System.Exception(ex.Message)
        Finally
            Close(conn)
            Close(cmd)
        End Try

    End Function
    ''' <summary>
    ''' 查询用户级别
    ''' </summary>
    ''' <param name="modelUserInfo"></param>
    ''' <returns>级别</returns>
    ''' <remarks></remarks>
    Public Function GetUserLevel(ByVal modelUserInfo As DataModel.m_UserInfo) As Object
        Dim sql As String = "UserInfo_GetUserLevel"
        Dim conn As New SqlConnection(connStr)
        Dim cmd As New SqlCommand(sql, conn)

        AddCmdParam(cmd, m_UserInfo.DBPARAM_USERID, modelUserInfo.userID)

        Try
            conn.Open()
            Return cmd.ExecuteScalar
        Catch ex As Exception
            Throw New System.Exception(ex.Message)
        Finally
            Close(conn)
            Close(cmd)
        End Try

    End Function 
    

业务逻辑层:

 '判断用户ID是否已存在
    Public Function IsUserIDExist(ByVal modelUserInfo As DataModel.m_UserInfo) As Boolean
        Try
            If IsNothing(sqlUserInfo.GetUserID(modelUserInfo)) Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    ''' <summary>
    ''' 判断密码是否正确
    ''' </summary>
    ''' <param name="modelUserInfo"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IsUserPwdRight(ByVal modelUserInfo As DataModel.m_UserInfo) As Boolean

        Try
            If IsNothing(sqlUserInfo.GetUserPwd(modelUserInfo)) Then
                Return False
            Else
                Return True
            End If

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    '调用数据访问层,获取用户级别
    Public Function GetUserLevel(ByVal modelUserInfo As DataModel.m_UserInfo) As String
        Dim strUserLevel As String
        Try
            If Not IsNothing(sqlUserInfo.GetUserLevel(modelUserInfo)) Then
                strUserLevel = sqlUserInfo.GetUserLevel(modelUserInfo).ToString
            Else
                strUserLevel = Nothing
            End If
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
        Return strUserLevel

    End Function


界面层:

        '将用户输入数据写入实体类(模板类)中
        modelUserInfo.userID = txtUserID.Text
        modelUserInfo.userPwd = txtUserPwd.Text

        Dim IsUserIDNull As Boolean
        Dim IsUserPwdRight As Boolean
        Dim strUserLevel As String

        Try
            IsUserIDNull = bllUserLogIn.IsUserIDExist(modelUserInfo)
        Catch ex As Exception
            Throw New System.Exception(ex.Message)
            Exit Sub
        End Try

        Select Case IsUserIDNull
            Case False
                MsgBox("用户ID错误,请重新输入!")
                txtUserID.Focus()
                Exit Sub
            Case True
                Try
                    IsUserPwdRight = bllUserLogIn.IsUserPwdRight(modelUserInfo)
                Catch ex As Exception
                    Throw New System.Exception(ex.Message)
                    Exit Sub
                End Try
            Case Else
                MsgBox("未知错误,可能由数据访问层:用户ID登录产生!")
                Exit Sub
        End Select

        '判断密码是否正确
        Select Case IsUserPwdRight
            Case False
                MsgBox("密码错误,请重新输入!")
                txtUserPwd.Focus()
                Exit Sub
            Case True
                Try
                    strUserLevel = bllUserLogIn.GetUserLevel(modelUserInfo)
                Catch ex As Exception
                    Throw New System.Exception(ex.Message)
                End Try
            Case Else
                MsgBox("未知错误,可能由数据访问层:用户密码产生!")
                Exit Sub
        End Select

        '判断用户级别
        Select Case strUserLevel
            Case "管理员"
                frmMain.Show()
            Case "操作员"

            Case "一般用户"

            Case Nothing
                MsgBox("没有获得用户级别,请检查UserInfo数据表!")
                Exit Sub
            Case Else
                MsgBox("未知错误,可能由数据访问层:用户级别产生!")
                Exit Sub
        End Select
    End Sub

       上面是各层基本的代码。会有一些不合理的地方,欢迎大家给指点并提出改进意见。这里的是最后写的代码,存储过程和其他的引用以及实体层常量的声明还有其他的一些方法省略了。      

       实现这个功能还有其他的方法,比如采用DataReader读取数据或者DataTable等,这里就不写了,下一篇博客总结一下常用的对象的使用方法,以及心得。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值