机房重构——三层的修改密码路线

在机房收费系统中,开始我是一个窗体一个功能的来写,后来慢慢的我开始划分成增的功能,改的功能,删的功能,和差的功能,先前我已经写了增删功能的,为了把这几种功能突出下,我就打算把最简单的修改部分写一下,写成一个系列的。由于修改学生信息的代码比较多,我就写一个修改密码的功能。

我先申明一下,我的CSDN使用不了代码编辑器和插不进图片,编辑的确不美观。等我换了系统再把格式编辑一下。

1,D层:我在修改密码中使用了两个函数,一个是我要知道用户的旧密码是否是正确的,另外一个是更改数据的密码。

(1)
    ''' <summary>
    ''' 判断旧口令是否正确
    ''' </summary>
    ''' <param name="UserID"></param>
    ''' <param name="OldPwd"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>


    Function dllQueryOldPwd(ByVal UserID As String, ByVal OldPwd As String) As Boolean
        Dim QueryOldPwdCmd As New SqlCommand
        QueryOldPwdCmd.Connection = coon

        Dim strSQL As String = "select * from T_UserInfo where userID= '" & UserID & "'"
        QueryOldPwdCmd = New SqlCommand(strSQL, coon)
        Dim QueryPwdRead As SqlDataReader
        Dim flagX As Boolean

        Try
            coon.Open()
            QueryPwdRead = QueryOldPwdCmd.ExecuteReader()
            QueryPwdRead.read()
            If QueryPwdRead.Item("userPassword") = OldPwd Then
                flagX = True
            Else
                flagX = False
            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())      '抛出异常对话框,显示到U层。ex是异常变量。
        Finally
            coon.Close()
            QueryOldPwdCmd = Nothing
        End Try
        Return flagX
    End Function


(2)

  ''' <summary> 
    ''' 修改密码
    ''' </summary>  
    ''' <param name="UserID" >字符串</param>
    ''' <param name="OldPwd ">字符串</param>
    ''' <param name="NewPwd">字符串</param>
    ''' <returns>返回true or false</returns> 
    ''' <remarks>通过执行时影响的行数判断是否执行成功</remarks>

    Public Function dllModifyPwd(UserID As String, OldPwd As String, NewPwd As String) As Boolean
        Dim ModifyCmd As New SqlCommand
        Dim count As Integer
        ModifyCmd.Connection = coon
        ModifyCmd.CommandText = "update T_UserInfo set
userPassword=@NewPassword whereuserId=@userID and userPassword=@OldPassword"
        ModifyCmd.Parameters.Add(New SqlParameter("@NewPassword", NewPwd))
        ModifyCmd.Parameters.Add(New SqlParameter("@userID", UserID))
        ModifyCmd.Parameters.Add(New SqlParameter("@OldPassword", OldPwd))

        Try
            coon.Open()
            count = ModifyCmd.ExecuteNonQuery()     '返回有影响的行数

            If count = 1 Then
                MsgBox("密码修改成功", vbOK + vbExclamation, "提示")
            Else
                MsgBox("密码错误,请重新输入密码!", vbOK + vbExclamation, "警告")
            End If
            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            coon.Close()
            ModifyCmd = Nothing
        End Try

 End Function


2,B层,我主要把从D层返回的Boolean进行了判断,if怎样else怎样,就是一个逻辑上的判断,然后再返回给U层。相对应D层我在B层也用了两个函数判断。

(1)

Public Class ModifyPwdBLL
''' <summary>
    ''' 判断旧口令是否正确
    ''' </summary>
    ''' <param name="loginUserID"></param>
    ''' <param name="OldPwd"></param>
    ''' <returns>返回布尔值</returns>
    ''' <remarks></remarks>
    '''
    Public Function bllQueryOldPwd(ByVal loginUserID As String, ByVal OldPwd As String) As Boolean
        Dim QueryOldPwd As New ChargeDAL.T_UserInfoDAL
        Dim flag As Boolean = QueryOldPwd.dllQueryOldPwd(loginUserID, OldPwd)
        ' Return flag
        If flag = True Then
            Return True
        Else
            ' MsgBox("用户密码错误,请重新确认用户密码是否填写正确", , "警告")
            Return False
        End If
    End Function   

(2)

 ''' <summary>
    ''' 更新密码
    ''' </summary>
    ''' <param name="loginUserID"></param>
    ''' <param name="NewPwd"></param>
    ''' <param name="OldPwd"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ModifyPwd(ByVal loginUserID As String, ByVal OldPwd As String, ByVal NewPwd As String) As Boolean
        Dim ModifyPwdInfo As New ChargeDAL.T_UserInfoDAL
        Dim flag As Boolean = ModifyPwdInfo.dllModifyPwd(loginUserID, OldPwd, NewPwd)
        If flag = True Then
            Return True
        Else
            Return False
        End If
    End Function
   

End Class


3,U层,最开始的时候我是先打了一点U层的代码到了要调用的时候我就开始调到了D层,从D层开始写着过来。这里我就开始时候前面说的处理文本框的模块,大家可以看看我是怎样调用的,因为我在调用这一块也出了点事故。调了三天的代码就是返回不出来模块的内容就是我我调用的时候把参数写错位了。

Public Class frmModifyPwd

    ''' <summary>
    ''' 定义关于三个文本框的数组函数Rdim()
    ''' </summary>
    ''' <remarks></remarks>
    '''
    Private Sub Radim()
        ReDim Preserve arrayControl(3)
        arrayControl(0) = New Term(txtOldPwd, "旧口令")
        arrayControl(1) = New Term(txtNewPwd, "新口令")
        arrayControl(2) = New Term(txtConfirmNewPwd, "确定新口令")
    End Sub


    ''' <summary>
    ''' 确定修改密码.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
        Dim modfifyNewPassword As String
        Dim modfifyoldPassword As String
        Dim uiLoginUserID As String

        modfifyNewPassword = txtNewPwd.Text.Trim()
        modfifyoldPassword = txtOldPwd.Text.Trim()
        uiLoginUserID = GlobalVariableUserInfo.UserInfo


        '调用Rdim()函数,判断是否为空
        Call Radim()
        If CheckIsEmpty(arrayControl) Then
            Exit Sub
        End If


        '判断2次输入的密码是否一致
        If txtNewPwd.Text <> txtConfirmNewPwd.Text Then
            MsgBox("两次输入的密码不一致,请重新输入!", vbOKOnly + vbInformation, "提示")
        End If

        '判断旧口令是否正确
        Dim uiQueryOldPwd As New ChargeBLL.ModifyPwdBLL
        Dim modfifyUserID As String
        modfifyUserID = GlobalVariableUserInfo.UserInfo
        Dim flag As Boolean = False
        flag = uiQueryOldPwd.bllQueryOldPwd(uiLoginUserID, txtOldPwd.Text)
        If flag = False Then
            MsgBox("旧口令输入错误!", vbOKOnly + vbInformation, "提示")
            txtOldPwd.Text = ""
            txtOldPwd.Focus()
            Exit Sub
        End If


        '更新新口令
        Dim modifyPwdInfo As New ChargeBLL.ModifyPwdBLL
        Dim flagX As Boolean = modifyPwdInfo.ModifyPwd(modfifyUserID, modfifyoldPassword, modfifyNewPassword)
        If flagX = True Then
            ' MsgBox("修改成功!", vbOKOnly + vbInformation, "提示")
            Exit Sub
            Me.Close()
        End If
    End Sub


    ''' <summary>
    ''' 退出
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        End
    End Sub
   
End Class


小结:

在修改窗体这一个窗体收货就是就是我开始学出来参数为三个的函数,哈哈,开始我在调用这一块吃了很多苦啊,就是因为函数没哟写对,参数没有传过来,另外就是没有好好把参数的位置调整好,搞得乱七八糟的,参数传过来的是错位的。


你可能感兴趣的:(机房重构——三层的修改密码路线)