我在Mysql连接中遇到此错误。代码用于更改密码。我已经尝试了很多,但我无法解决错误。拜托,任何人都可以帮忙。
Imports MySql.Data.MySqlClient
Imports System.Security.Cryptography
Imports System.Text
Imports System.Data
Public Class frmchpwd
Dim myconnection As MySqlConnection(ConnectionString)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub btnProfileChangePwd_Click(sender As Object, e As EventArgs) Handles btnProfileChangePwd.Click
Try
Dim RowsAffected As Integer = 0
If Len(Trim(OldPassword.Text)) = 0 Then
MessageBox.Show("Please enter old password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
OldPassword.Focus()
Exit Sub
End If
If Len(Trim(NewPassword.Text)) = 0 Then
MessageBox.Show("Please enter new password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
NewPassword.Focus()
Exit Sub
End If
If Len(Trim(ConfirmPassword.Text)) = 0 Then
MessageBox.Show("Please confirm new password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
ConfirmPassword.Focus()
Exit Sub
End If
If NewPassword.TextLength < 5 Then
MessageBox.Show("The New Password Should be of Atleast 5 Characters", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
NewPassword.Text = ""
ConfirmPassword.Text = ""
NewPassword.Focus()
Exit Sub
ElseIf NewPassword.Text <> ConfirmPassword.Text Then
MessageBox.Show("Password do not match", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error)
NewPassword.Text = ""
OldPassword.Text = ""
ConfirmPassword.Text = ""
OldPassword.Focus()
Exit Sub
ElseIf OldPassword.Text = NewPassword.Text Then
MessageBox.Show("Password is same.. Re-enter new password", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error)
NewPassword.Text = ""
ConfirmPassword.Text = ""
NewPassword.Focus()
Exit Sub
End If
If myconnection.State <> ConnectionState.Open Then
myconnection.Open()
End If
Dim hash, hashold As String
Using md5Hash As MD5 = MD5.Create()
hash = GetMd5Hash(md5Hash, NewPassword.Text)
hashold = GetMd5Hash(md5Hash, OldPassword.Text)
End Using
Dim co As String = "UPDATE logininfo_tb SET password = '" & hash & "' WHERE user_id ='" & SqlSafe(curent_user_id) & "' and password = '" & hashold & "';"
Dim cmd = New MySqlCommand(co)
cmd.Connection = myconnection
RowsAffected = cmd.ExecuteNonQuery()
If RowsAffected > 0 Then
NewPassword.Text = ""
OldPassword.Text = ""
ConfirmPassword.Text = ""
OldPassword.Focus()
'tracksystemlogs("Changed Password")
MessageBox.Show("Successfully changed", "Password", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Close()
Else
MessageBox.Show("invalid email or password", "input error", MessageBoxButtons.OK, MessageBoxIcon.Error)
NewPassword.Text = ""
OldPassword.Text = ""
ConfirmPassword.Text = ""
OldPassword.Focus()
End If
If myconnection.State = ConnectionState.Open Then
myconnection.Close()
End If
Catch ex As Exception
If ex.Message.Contains("MySQL hosts") Then
MessageBox.Show("Server not found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Try
End Sub
Shared Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String
' Convert the input string to a byte array and compute the hash.
Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))
' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To data.Length - 1
sBuilder.Append(data(i).ToString("x2"))
Next i
' Return the hexadecimal string.
Return sBuilder.ToString()
End Function 'GetMd5Hash
End Class
答案 0 :(得分:0)
嘿伙计们我终于解决了它,在它应该是MySqlConnection
Dim myconnection As New MySqlConnection(ConnectionString)。希望每个人都能幸运。感谢。