我正在寻找可以帮助我检查 MySQL 中的特定列并返回其值(如果它已经存在)的代码。我正在开发 ForgotPassword 模块,因此当用户单击“忘记密码”时,会出现一个表单,要求用户输入他/她的用户名。一旦他/她完成,它将检查系统以查看输入的用户名是否存在。我在 Stack Overflow 上找到了一些代码:
Private Sub btnCheckUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckUser.Click
If IsUserExist(userName:=True) Then
MsgBox("user exists")
Else
MsgBox("user does not exists")
End If
End Sub
Private Function IsUserExist(ByVal userName As String) As Boolean
Dim query As String
Dim returnValue As Boolean = False
query = "SELECT username FROM dbase.tblusers WHERE username = @username "
Using conn As New MySqlConnection("server=localhost; userid=root; password=root; database=dbase")
Using cmd As New MySqlCommand()
With cmd
.Connection = conn
.CommandText = query
.CommandType = CommandType.Text
.Parameters.AddWithValue("@username", txtUsername.Text)
End With
Try
conn.Open()
If CInt(cmd.ExecuteScalar()) > 0 Then
returnValue = True
End If
Catch ex As MySqlException
MsgBox(ex.Message)
returnValue = False
Finally
conn.Close()
End Try
End Using
End Using
Return returnValue
End Function
但是这段代码给了我一个错误Conversion from string "username" to type 'Integer' is not valid
。If CInt(cmd.ExecuteScalar()) > 0 Then