机房收费系统重构之修改密码,算是自己独立敲出的第一条线,也代表着自己对三层终于有了一定的理解。
修改密码主要思路:
活动图:
三层思想:
UI层:
负责将用户输入信息记录到实体层。
BLL层:
负责判断旧密码是否正确,输入的两次新密码是否相同。
DAL层:
负责从数据库调取数据,修改数据。
在过程中,遇到的一个困难:如何将登陆的用户名记录下来,以供修改密码查找记录时调用。
vb中是在模板里定义了一个公共变量,这里应该也是这种思想....但如何定义呢?
解决方法:
用share关键字。
在实体中:
Public Shared UserID As String
这样就可实现将变量值应用于整个程序。
最初想到这个问题(如何设定一个公共变量),我先想到了:如何让一个变量被赋完值,不再变化(实例化后不是空)。然后我首先想到的是static关键字,static关键字是在C++考试时接触较多的。但我也只知道给static类变量赋值后,将保留最新值。当然实验的结果是:用在这里是不合适的,所以又找到了share。
static为什么不行,问题在哪,它和share有什么区别?我总结了一篇博客:在这里有答案......
http://blog.csdn.net/onlybymyself/article/details/44726719
下面是修改密码的代码及一些注释(VB.NET版):
实体层:
Public Class e_UserPwd Public Shared UserID As String '用于记录登录时的用户名 Public Shared OldPwd As String '用于记录输入的旧密码 Private e_Password As String Private e_NewPwd1 As String Private e_NewPwd2 As String Public Property Password As String Get Return e_Password End Get Set(ByVal value As String) e_Password = value End Set End Property Public Property NewPwd1 As String Get Return e_NewPwd1 End Get Set(ByVal value As String) e_NewPwd1 = value End Set End Property Public Property NewPwd2 As String Get Return e_NewPwd2 End Get Set(ByVal value As String) e_NewPwd2 = value End Set End Property End Class
DAL:
'引用命名空间 Imports System.Data.SqlClient Imports ChargeEntity '连接数据库,查询数据 Public Class Changpassword Dim sqlConnectStr As String = "server=zhaohan;database=ChargeSystem;uid=sa;pwd=123456;" Dim sqlConnection1 As SqlConnection = New SqlConnection(sqlConnectStr) 'SqlConnection:重新连接 Function selectPassword(ByVal User As ChargeEntity.e_UserPwd) As ChargeEntity.e_UserPwd Dim sql As String = "select * from User_Info where UserID='" & ChargeEntity.e_UserPwd.UserID & "'" Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1) '定义sqlcommand对象 Dim read As SqlDataReader '定义sqldatareader对象 Dim User1 As New ChargeEntity.e_UserPwd Dim UserDaTable As New DataTable Try sqlConnection1.Open() read = cmd.ExecuteReader() UserDaTable.Load(read) User1.Password = UserDaTable.Rows(0)("UserPWD") Return User1 Catch ex As Exception '如果出现错误则执行 User1.Password = "" Return User1 Finally If Not IsNothing(sqlConnection1) Then sqlConnection1.Close() '关闭数据库连接 End If End Try End Function '修改密码 Function chagpsd(ByVal pasd As ChargeEntity.e_UserPwd) As Boolean Dim sql As String = "update User_Info set UserPWD='" & pasd.NewPwd1 & "'where UserID='" & ChargeEntity.e_UserPwd.UserID & "'" ' ChargeEntity.e_UserPwd.UserID share类型如此引用 Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1) cmd.CommandText = sql cmd.CommandType = CommandType.Text sqlConnection1.Open() Dim flag As Boolean '用于判断数据库是否正确执行操作更新 flag = cmd.ExecuteNonQuery If flag = True Then Return True Else Return False End If sqlConnection1.Close() End Function End Class
BLL:
Public Class Changpassword '判断旧密码是否正确 Function CheckoldPassword(ByVal Message As ChargeEntity.e_UserPwd) As Boolean Dim OdPsd As New ChargeDAL.Changpassword Dim e_msg As New ChargeEntity.e_UserPwd e_msg = OdPsd.selectPassword(Message) '将D层获取的数据库信息传给实体e_msg If ChargeEntity.e_UserPwd.OldPwd = e_msg.Password Then '输入密码正确返回True,否则返回False Return True Else Return False End If End Function '判断两次密码是否相同 Function isequal(ByVal psd As ChargeEntity.e_UserPwd) As Boolean Dim Dpsd As New ChargeDAL.Changpassword If psd.NewPwd1 = psd.NewPwd2 Then Return Dpsd.chagpsd(psd) Else Return False End If End Function End Class
UI:
Public Class frmchangpassword Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim e_psd As New ChargeEntity.e_UserPwd Dim b_psd As New ChargeBLL.Changpassword '密码不得为空 If txtnewpsd.Text = "" Then MsgBox("密码不得为空") txtnewpsd.Focus() End If ChargeEntity.e_UserPwd.OldPwd = txtodpsd.Text If b_psd.CheckoldPassword(e_psd) = True Then '如果旧密码填写正确 e_psd.NewPwd1 = txtnewpsd.Text e_psd.NewPwd2 = txtnewpsd2.Text If b_psd.isequal(e_psd) = False Then '如果两次输入新密码不同 MsgBox("两次输入密码不同,请重新输入") txtnewpsd.Text = "" txtnewpsd2.Text = "" txtnewpsd.Focus() Else MsgBox("密码修改成功") Me.Close() End If Else MsgBox("原始密码有误,请重新输入") End If End Sub End Class
总结:机房收费系统,虽然很慢,但收获很多,内心虽然焦急,但切不可盲目追求进度。虽说不可盲目追求进度.................但也不能一直停步不前呐,赶紧着吧......加油,表灰心~~~