?? chagepwd.vb
字號:
?Imports System.Data
Imports System.Data.SqlClient '新增命名空間 for SQL Server
Public Class ChagePwd
Dim TableName As String = "Login"
Dim errorMsg As String
Dim LoginErrorCounter As Integer
Dim ConnString As String
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim da As SqlDataAdapter
Dim UserID As String
Dim OldPwd As String
Dim NewPwd As String
Dim CheckNewPwd As String
#Region "資料庫初始化"
''' <summary>
''' 資料庫初始化
''' </summary>
Public Sub InitDB()
ConnString = SQLConnectionString()
conn = New SqlConnection(ConnString)
conn.Open()
End Sub
#End Region
'確定
Private Sub btn_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OK.Click
UserID = WinEvents.LoginUserID
OldPwd = txt_OldPwd.Text
NewPwd = txt_NewPwd.Text
CheckNewPwd = txt_CheckNewPwd.Text
If CheckField() Then
If RunChagePwd(UserID, OldPwd, NewPwd) Then
Me.Close()
End If
End If
End Sub
#Region "欄位檢查"
''' <summary>
''' 欄位檢查
''' </summary>
Public Function CheckField() As Boolean
If (txt_OldPwd.Text = "") Then
MessageBox.Show("[舊密碼]不得為空白;", "欄位檢查", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
txt_OldPwd.Text = ""
txt_NewPwd.Text = ""
txt_CheckNewPwd.Text = ""
txt_OldPwd.Focus()
Return False
End If
If (txt_NewPwd.Text = "") Then
MessageBox.Show("[新密碼]不得為空白;", "欄位檢查", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
txt_OldPwd.Text = ""
txt_NewPwd.Text = ""
txt_CheckNewPwd.Text = ""
txt_NewPwd.Focus()
Return False
End If
If (txt_CheckNewPwd.Text = "") Then
MessageBox.Show("[確認密碼]不得為空白;", "欄位檢查", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
txt_OldPwd.Text = ""
txt_NewPwd.Text = ""
txt_CheckNewPwd.Text = ""
txt_CheckNewPwd.Focus()
Return False
End If
If (txt_NewPwd.Text <> txt_CheckNewPwd.Text) Then
MessageBox.Show("[新密碼]與[確認密碼]必須一樣;", "欄位檢查", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
txt_OldPwd.Text = ""
txt_NewPwd.Text = ""
txt_CheckNewPwd.Text = ""
txt_NewPwd.Focus()
Return False
End If
Return True
End Function
#End Region
#Region "執行更改動作"
Public Function RunChagePwd(ByVal UserId As String, ByVal OldPwd As String, ByVal NewPwd As String) As Boolean
If UpdatePwd(UserId, OldPwd, NewPwd) Then
If VerifyPwd(UserId, NewPwd) Then
MessageBox.Show("使用者密碼變更完成^_^")
txt_OldPwd.Text = ""
txt_NewPwd.Text = ""
txt_CheckNewPwd.Text = ""
Return True
Else
MessageBox.Show("使用者密碼變更失敗>.<")
Return False
End If
Return True
Else
MessageBox.Show("使用者密碼變更失敗>.<")
Return False
End If
End Function
#End Region
#Region "更新密碼"
''' <summary>
''' 更新密碼
'''</summary>
Public Function UpdatePwd(ByVal UserId As String, ByVal OldPwd As String, ByVal NewPwd As String) As Boolean
InitDB()
Dim updateCmd As String
Dim i As Integer = 0
updateCmd = "UPDATE " + TableName + " SET UserPwd='" + NewPwd + "'"
updateCmd = updateCmd + " WHERE UserID='" + UserId + "'"
Try
cmd = New SqlCommand(updateCmd, conn)
i = cmd.ExecuteNonQuery() '若沒有任何異動則回傳0
conn.Close()
If (i = 1) Then
Return True
Else
Return False
End If
Catch ex As Exception
errorMsg = ex.Message
conn.Close()
Return False
End Try
Return False
End Function
#End Region
#Region "驗證帳號與密碼"
Public Function VerifyPwd(ByVal UserID As String, ByVal UserPwd As String) As Boolean
InitDB()
Dim selectCmd As String
selectCmd = "Select * From " + TableName + " Where UserID='" + UserID + "' And UserPwd='" + UserPwd + "'"
'注意:欄位名稱不可為Password
Try
cmd = New SqlCommand(selectCmd, conn)
dr = cmd.ExecuteReader()
If (dr.Read()) Then
conn.Close()
Return True
Else
conn.Close()
Return False
End If
Catch ex As Exception
errorMsg = ex.Message
conn.Close()
Return False
End Try
End Function
#End Region
Private Sub btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cancel.Click
Me.Close()
End Sub
End Class
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -