如何在VB.net中创建多个MySqlDataReader If语句?

时间:2016-05-11 06:00:33

标签: vb.net mysqldatareader

你好我想用多个MySQLdatareader制作if语句。我不知道它会起作用。请帮我修复我的代码。这是我的示例代码。

  Sub Emailusernameexist()
Dim sCommand1 As MySqlCommand = New MySqlCommand("SELECT * FROM account WHERE Email = '" & TextBox5.Text & "'", conn)
Dim sCommand2 As MySqlCommand = New MySqlCommand("SELECT * FROM account WHERE Username = '" & TextBox4.Text & "'", conn)
RD1 = sCommand1.ExecuteReader
RD2 = sCommand2.ExecuteReader
Try
    If RD1.HasRows Then
        While RD1.Read
            Dim cname As String
            cname = RD1.GetString("Completename")
            MsgBox("Sorry, your email have been registered by " + cname + "? Please login !", vbInformation)
        End While
    ElseIf RD2.HasRows Then
        While RD2.Read
            Dim cname As String
            cname = RD2.GetString("Completename")
            MsgBox("Maaf, username was owned by  " + cname + " !", vbInformation)
        End While
    End If
Catch ex As Exception
End Try
conn.close()
End Sub

1 个答案:

答案 0 :(得分:0)

我建议使用以下代码。我没试过,但这是一般的想法。

    Sub Emailusernameexist()
        Dim sCommand As MySqlCommand = New MySqlCommand("SELECT Email, Username, Completename FROM account WHERE Email = '" & TextBox5.Text & "'" OR Username = '" & TextBox4.Text, conn)
        RD = sCommand.ExecuteReader

        Try
            If RD.HasRows Then
                While RD.Read
                    Dim email as String
                    Dim userName as String
                    Dim cname As String

                    email = RD.GetString("email")
                    userName = RD.GetString("userName")
                    cname = RD.GetString("Completename")

                    if (not IsNull(email)) Then 
                        MsgBox("Sorry, your email have been registered by " + cname + "? Please login !", vbInformation)
                    elseif (not IsNull(userName)) Then 
                        MsgBox("Maaf, username was owned by  " + cname + " !", vbInformation)
                    endif
                End While
        Catch ex As Exception
        End Try
        conn.close()
    End Sub