vb.net не могу подключиться к mysql

я пытался создать на логин систему с mysql. но я не знаю, что это не работает. Это дает мою попытку снова каждый раз, что проблема?

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    mysqlconn = New MySqlConnection
    mysqlconn.ConnectionString = "server=localhost;userid=root;password=;database=tes"
    Dim reader As MySqlDataReader

    Try
        mysqlconn.Open()
        Dim query As String = "select * from kullanicilar where 'username=" & TextBox1.Text & "' and 'password=" & TextBox2.Text & "'"
        komut = New MySqlCommand(query, mysqlconn)
        reader = komut.ExecuteReader
        Dim girdimi As Integer = 0

        If reader.Read Then
            girdimi = girdimi + 1
        End If

        If girdimi = 1 Then
            Form2.Show()
        ElseIf girdimi = 0 Then
            MessageBox.Show("try again")
        End If
        mysqlconn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        mysqlconn.Dispose()

    End Try
End Sub

1 ответ

  1. Попробуйте сделать это следующим образом (на основе статьи DataReader MSDN):

       Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        mysqlconn = New MySqlConnection
        mysqlconn.ConnectionString = "server=localhost;userid=root;password=;database=tes"
        Dim reader As MySqlDataReader
    
        Try
            mysqlconn.Open()
            Dim query As String = "select * from kullanicilar where username='" & TextBox1.Text & "' and password='" & TextBox2.Text & "'"
            komut = New MySqlCommand(query, mysqlconn)
            reader = komut.ExecuteReader()
            Dim girdimi As Integer = 0
    
            If reader.HasRows Then
                Do While reader.Read()
                    girdimi = girdimi + 1
                    If girdimi = 1 Then
                        Form2.Show()
                    ElseIf girdimi = 0 Then
                        MessageBox.Show("try again")
                   End If
            Loop
            End If
    
            mysqlconn.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            mysqlconn.Dispose()
    
        End Try
    End Sub