Visual Basic行/列中没有数据

时间:2018-10-24 08:20:16

标签: mysql vb.net visual-studio-2017

我一直在搜索有关此问题的许多参考资料,但我一无所获。它仍然存在相同的错误,当我登录时,它将没有任何问题,并且过一会儿,将显示该错误很多次。

Call Koneksi()
    Try
        cmd = New Odbc.OdbcCommand("SELECT * FROM tb_user WHERE username = '" & txt_username.Text & "' AND password = '" & txt_password.Text & "'", conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows = True Then
            txt_akses.Text = rd!hak_akses
            MsgBox("Welcome '" & rd!nama & "'", MsgBoxStyle.Exclamation, "Information")
            MenuCustomer.txt_user.Text = rd!username
            cmd = New Odbc.OdbcCommand("SELECT * FROM tb_permainan WHERE status = 1", conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If txt_akses.Text = "Customer" Then
                If rd.HasRows = False Then
                    txt_username.Text = ""
                    txt_password.Text = ""
                    txt_akses.Text = ""
                    MenuCustomer.Show()
                Else
                    MsgBox("Permainan sedang berlangsung. Mohon Untuk login sesaat lagi", MsgBoxStyle.Exclamation, "Information")
                End If
            Else
                MenuStaff.Show()
            End If
        Else
            txt_username.Text = ""
            txt_password.Text = ""
            txt_akses.Text = ""
            txt_username.Select()
            MsgBox("Username atau password salah", MsgBoxStyle.Exclamation, "Information")
        End If
        rd.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

这是我的名为koneksi的模块     导入System.Data.Odbc

Module CRUD
'Setting Koneksi
Public conn As OdbcConnection
Public cmd As OdbcCommand
Public ds As New DataSet
Public da As OdbcDataAdapter
Public rd As OdbcDataReader
Public dt As New DataTable
Public LokasiData As String
Public result As String
Sub Koneksi()
    LokasiData = "Driver={MySQL ODBC 3.51 Driver};Database=db_bubble;server=192.168.1.14;uid=root"
    conn = New OdbcConnection(LokasiData)
    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

DataReader.HasRows属性检查必须在DataReader.Read()方法之前,该方法应包含在While循环中,因为DataReader是仅向前流(检查{ {1}})。下面的示例是正确的方法:

ExecuteReader()

请注意,在这种情况下,您应该使用不同的Try cmd = New Odbc.OdbcCommand("SELECT * FROM tb_user WHERE username = @UserName AND password = @Password", conn) cmd.Parameters.AddWithValue("@UserName", txt_username.Text) cmd.Parameters.AddWithValue("@Password", txt_password.Text) Using rd As OdbcDataReader = cmd.ExecuteReader() If rd.HasRows = True Then While rd.Read() txt_akses.Text = rd!hak_akses MsgBox("Welcome '" & rd!nama & "'", MsgBoxStyle.Exclamation, "Information") MenuCustomer.txt_user.Text = rd!username cmd2 = New Odbc.OdbcCommand("SELECT * FROM tb_permainan WHERE status = 1", conn) Using rd2 As OdbcDataReader = cmd2.ExecuteReader() If rd2.HasRows = True Then While rd2.Read() If txt_akses.Text = "Customer" Then If rd.HasRows = False Then txt_username.Text = "" txt_password.Text = "" txt_akses.Text = "" MenuCustomer.Show() Else MsgBox("Game is in progress. Please login for a while", MsgBoxStyle.Exclamation, "Information") End If Else MenuStaff.Show() End If End While End If End Using End While Else txt_username.Text = "" txt_password.Text = "" txt_akses.Text = "" txt_username.Select() MsgBox("Incorrect username or password", MsgBoxStyle.Exclamation, "Information") End If End Using Catch ex As Exception MsgBox(ex.Message) End Try 实例,因为在当前代码中,第二个DataReader分配会覆盖第一个分配。还可以在查询中使用参数以防止尝试进行SQL注入。

答案 1 :(得分:0)

我已经修改了我的代码,但仍然显示错误

        Using rd As OdbcDataReader = cmd.ExecuteReader
            If rd.HasRows = True Then
                While rd.Read()
                    txt_akses.Text = rd!hak_akses
                    MsgBox("Welcome '" & rd!nama & "'", MsgBoxStyle.Exclamation, "Information")
                    MenuCustomer.txt_user.Text = rd!username
                    If txt_akses.Text = "Customer" Then
                        cmd2 = New OdbcCommand("SELECT * FROM tb_permainan WHERE status = 1", conn)
                        Using rd2 As OdbcDataReader = cmd2.ExecuteReader
                            If rd2.HasRows = True Then
                                MsgBox("Permainan sedang berlangsung. Mohon Untuk login sesaat lagi", MsgBoxStyle.Exclamation, "Information")
                            Else
                                While rd2.Read()
                                    txt_username.Text = ""
                                    txt_password.Text = ""
                                    txt_akses.Text = ""
                                    MenuCustomer.Show()
                                End While
                            End If
                        End Using
                    Else
                        MenuStaff.Show()
                    End If
                End While
            Else
                txt_username.Text = ""
                txt_password.Text = ""
                txt_akses.Text = ""
                txt_username.Select()
                MsgBox("Username atau password salah", MsgBoxStyle.Exclamation, "Information")
            End If
        End Using

显示的错误正在循环。