vb.net无法连接到mysql

时间:2016-10-05 10:52:04

标签: mysql vb.net

我尝试使用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 个答案:

答案 0 :(得分:0)

以这种方式尝试(基于DataReader MSDN article):

   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