如何检查MySQL中字段中的特定值是否存在

时间:2014-12-31 10:05:53

标签: mysql vb.net forgot-password executescalar

我正在寻找可以帮助我检查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

2 个答案:

答案 0 :(得分:1)

MySqlCommand.ExecuteScalar返回查询返回的第一行的第一列。这意味着(对于您的实际查询)NULL(如果用户名不存在)或具有相同用户名的字符串作为where条件的参数传递。在任何情况下都不是整数 所以你只需要检查返回的对象是否为null(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配置设置为Off 使用此配置,布尔值True将自动转换为IsUserExist中参数所需的数据类型。所以这个函数接收参数username等于文字字符串“True”。当您尝试在表格中搜索某个真实用户名时,这并不是很有用 您似乎需要在buttonLlick代码中的某个位置获取该值,可能来自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)
   ....

答案 1 :(得分: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