憋屈了几天,终于将这个功能实现了。理清了思路,一切都那么容易了
下面我们来看一下机房重构修改密码的代码吧
<span style="color:#333333;">Imports System.Windows.Forms Imports System.Drawing Imports Model Public Class frmChangePassword Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Me.Hide() frmMain.Show() End Sub Private Sub frmChangePassword_Load(sender As Object, e As EventArgs) Handles MyBase.Load txtOldPassword.Select() txtOldPassword.Focus() Label1.Parent = PictureBox1 Label1.BackColor = Color.Transparent Label2.Parent = PictureBox1 Label2.BackColor = Color.Transparent Label3.Parent = PictureBox1 Label3.BackColor = Color.Transparent End Sub Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click '进行非空判断 旧密码不能为空 If txtOldPassword.Text = "" Then MsgBox("请输入原始密码", 0, "提示") txtOldPassword.Select() txtOldPassword.Focus() End If '新密码不能为空 If txtNewPassword.Text = "" Then MsgBox("请输入新密码", 0, "提示") txtNewPassword.Select() txtNewPassword.Focus() End If ' 确认新密码不能为空 If txtBeSure.Text = "" Then MsgBox("请确认新密码", 0, "提示") End If '判断旧密码是否正确 Dim fac As New Facade.LoginFacade '借用登录窗体的密码 Dim userInfo As New Model.UserModel userInfo.UserID = frmLogin.txtUserID.Text.Trim() userInfo.Password = txtOldPassword.Text.Trim() Dim table As New DataTable table = fac.CheckPwd(userInfo) If txtOldPassword.Text <> Trim(table.Rows(0).Item(1)) Then MsgBox("旧密码不正确,请查证后再输入", "0", "提示") txtOldPassword.Text = "" txtNewPassword.Text = "" txtBeSure.Text = "" txtOldPassword.Focus() End If '判断两次输入的新密码是否相同 If txtNewPassword.Text <> txtBeSure.Text Then MsgBox("您两次输入的密码不相同", 0, "提示") txtBeSure.Text = "" txtNewPassword.Text = "" txtNewPassword.Select() txtNewPassword.Focus() Else '进行密码更新 Dim Updatefac As New Facade.ChangePasswordFacade Dim table1 As New Boolean Dim UserPwd As New Model.UserModel UserPwd.Password = txtNewPassword.Text.Trim() UserPwd.UserID = frmLogin.txtUserID.Text.Trim() table1 = Updatefac.ChangePwd(UserPwd) If table1 = False Then MsgBox("修改失败", "0", "提示") Else MsgBox("修改成功", 0, "恭喜哦亲") End If End If End Sub End Class</span>
Imports Model Imports BLL Imports System.Reflection Public Class ChangePasswordFacade Public Function ChangePwd(ByVal UserPwd As Model.UserModel) As Boolean Dim cUser As New BLL.Changepassword Dim flag As New Boolean flag = cUser.ChangePwd(UserPwd) Return flag End Function End Class
Imports Factory Imports IDAL Public Class Changepassword Public Function ChangePwd(ByVal Userpwd As Model.UserModel) As Boolean Dim fac As New Factory.ChangePwdFactory Dim iChangepwd As IDAL.IChangePassword iChangepwd = fac.Changepwd Dim dt As Boolean dt = iChangepwd.ChangePassword(Userpwd) Return dt End Function End Class
Imports SQLHelper Public Class ChangePasswordDAL : Implements IDAL.IChangePassword Private shelper As New SQLHelper.SqlHelper Public Function ChangePassword(ByVal UserPwd As UserModel) As Boolean Implements IChangePassword.ChangePassword ' Dim shelper As New SQLHelper.SqlHelper Dim paras As SqlParameter() = {New SqlParameter("@Password", UserPwd.Password), New SqlParameter("@UserID", UserPwd.UserID)} Dim cmdText As String cmdText = "update User_info set Password=@Password where UserID=@UserID" Dim dt As Boolean dt = shelper.ExecAddDelUpdate(cmdText, CommandType.Text, paras) Return dt End Function End Class
Imports System.Reflection Public Interface IChangePassword Function ChangePassword(ByVal UserPwd As Model.UserModel) As Boolean End Interface
Imports System.Reflection Imports System.Configuration Imports IDAL Public Class ChangePwdFactory '修改密码 Dim strDB As String = System.Configuration.ConfigurationManager.AppSettings("DB") Public Function Changepwd() As IDAL.IChangePassword 'CType函数将返回表达式显示地转换为指定的数据类型、对象、结构、类或接口的结果 Return CType(Assembly.Load("DAL").CreateInstance("DAL.ChangePasswordDAL"), IDAL.IChangePassword) End Function End Class
Public Class UserModel Private _UserID As String Private _Password As String Private _Level As String Private _UserName As String Private _Head As String Public Property UserName() As String Get Return _UserName End Get Set(value As String) _UserName = value End Set End Property Public Property UserID() As String Get Return _UserID End Get Set(value As String) _UserID = 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 Public Property Level() As String Get Return _Level End Get Set(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 End Class
总结:
原来师傅让用数据库的表作为一个实体还有点不理解,现在终于明白了,一个数据表作为一个实体,那样层次就更加清楚了。祝愿自己在这条路上越走越顺