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 validを出しIf CInt(cmd.ExecuteScalar()) > 0 Thenます。

4

2 に答える 2

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 に答える