0

我的登录系统遇到问题。这是我的编码:我已经在我的表单中添加了引用 MySQL.Data。当我单击按钮时,会显示此错误消息。(在 mscorlib.dll 中发生了“System.Collections.Generic.KeyNotFoundException”类型的第一次机会异常)

Imports MySql.Data.MySqlClient
Public Class Form1
    Public conn As MySqlConnection
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        conn = New MySqlConnection("server=localhost; user id=root; password=; database=test")
        Try
            conn.Open()
            Dim sqlquery As String = "SELECT * FROM yy WHERE Username = '" & TextBox1.Text & "';"
            Dim data As MySqlDataReader
            Dim adapter As New MySqlDataAdapter
            Dim command As New MySqlCommand
            command.CommandText = sqlquery
            command.Connection = conn
            adapter.SelectCommand = command
            data = command.ExecuteReader
            While data.Read()
                If data.HasRows() = True Then
                    If data(2).ToString = TextBox2.Text Then
                        MsgBox("Login")
                    Else
                        MsgBox("Failed Login")

                    End If
                Else
                    MsgBox("Failed Login")
                End If
            End While

        Catch ex As Exception

        End Try
    End Sub
End Class
4

0 回答 0