visual basic行/列不存在数据

4xrmg8kj  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(403)

我找了很多关于这个问题的参考资料,但什么都没有。。它仍然有相同的错误,当我做登录,然后它将不会有任何问题,并在一段时间后,错误会显示这么多次。

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 imports 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
carvr3hs

carvr3hs1#

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

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

显示的错误是循环。。

raogr8fs

raogr8fs2#

这个 DataReader.HasRows 属性检查必须在 DataReader.Read() 方法,该方法应包含在 While 循环因为 DataReader 是只向前的流(在之后立即检查行可用性) 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 示例,因为在当前代码中 DataReader 赋值覆盖第一个。还可以在查询中使用参数来防止sql注入尝试。

相关问题