1

我正在寻找可以帮助我检查 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 validIf CInt(cmd.ExecuteScalar()) > 0 Then

4

2 回答 2

1

MySqlCommand.ExecuteScalar返回查询返回的第一行的第一列。这意味着(对于您的实际查询)NULL(如果用户名不存在)或具有相同用户名的字符串作为 where 条件的参数传递。在任何情况下都不是整数。
所以你只需要检查返回的对象是否为空(在 VB.NET 中为空)。

Dim result = cmd.ExecuteScalar()
if result IsNot Nothing then
  ... user exists....

解决您的问题的另一种方法可能是取回包含您的用户名的行数的 COUNT

query = "SELECT COUNT(*) FROM dbase.tblusers WHERE username = @username"

然后,将 , 的返回值转换为整数ExecuteScalar将起作用

最后,关于语法If IsUserExist(userName:=True) Then

这可能只是因为您将项目的Option Strict配置设置为关闭。
使用此配置,布尔值True将自动转换为 in 中参数预期的数据类型IsUserExist。所以这个函数接收的参数username等于字符串“True”。当您尝试在表中搜索一些真实用户名时,它并不是很有用。
似乎您需要在 buttonclick 代码中的某处获取该值,可能来自 TextBox。

Private Sub btnCheckUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckUser.Click
    Dim userName = txtUsername.Text
    If IsUserExist(userName) Then
        MsgBox("user exists")
    Else
        MsgBox("user does not exists")
    End If
End Sub

当然,现在你应该使用userName接收到的变量IsUserExist来初始化参数

With cmd
   .Parameters.AddWithValue("@username", userName)
   ....
于 2014-12-31T10:13:40.110 回答
0
Private Sub btnCheckUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckUser.Click
    If IsUserExist(txtUsername.Text) Then #Use value from textbox on your form, use '#' for comments
        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", userName) # I think here should user value from function's parameter, not from textbox
            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
于 2014-12-31T10:19:54.897 回答