0

データベースクエリの後に実行する必要がある埋め込みIFステートメントがあります。ただし、実行時に、ステートメント全体がまったく評価されていないことに気付きました(直後のステートメントWhile dbData.Read())。私は何が間違っているのですか?

コードは次のとおりです。

Private Sub ButtonNew_Click(ByVal sender As System.Object, ByVal e As     System.EventArgs) Handles ButtonNew.Click

If TextBoxSearch.Text = "" Then
        MessageBox.Show("Sorry, you must enter an ACCOUNT# before proceeding!")
        TextBoxSearch.Focus()
    Else
        Try
            Dim dbConn As MySqlConnection
            dbConn = New MySqlConnection("Server=" & FormLogin.ComboBoxServerIP.SelectedItem & ";Port=3306;Uid=parts;Password=parts;Database=accounting")
            Dim account As Boolean = True
            If dbConn.State = ConnectionState.Open Then
                dbConn.Close()
            End If
            dbConn.Open()
            Dim dbQuery As String = "SELECT * FROM customer WHERE accountNumber = '" & TextBoxSearch.Text & "';"
            Dim dbData As MySqlDataReader
            Dim dbAdapter As New MySqlDataAdapter
            Dim dbCmd As New MySqlCommand
            dbCmd.CommandText = dbQuery
            dbCmd.Connection = dbConn
            dbAdapter.SelectCommand = dbCmd
            dbData = dbCmd.ExecuteReader
            While dbData.Read()
                If dbData.HasRows Then
                    'MessageBox.Show("Customer record already exists!")
                    Call recordExists()
                    account = False
                    Call lockForm()
                    TextBoxSearch.Focus()
                    Me.Refresh()
                Else
                    'dbData.Close()
                    account = True
                    TextBoxAccount.Text = TextBoxSearch.Text
                    TextBoxAccount.BackColor = Color.LightGray
                    TextBoxAccount.TabStop = False
                    Call unlockForm()
                    TextBoxLastName.Focus()
                    ButtonSubmit.Visible = True
                    Me.Refresh()
                End If
            End While
            dbData.Close()
            dbCmd.Dispose()
            dbAdapter.Dispose()
            dbConn.Close()
        Catch ex As Exception
            MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
                        vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
        End Try
    End If

End Sub
4

1 に答える 1

1

dbData.Read()を返す場合False、ループに入らないため、Ifステートメントは実行されません。読み取る行がない場合は、Read常にを返すFalseため、Ifステートメントはどこにあるかは役に立ちません。whileループがブロックIf内に収まるように、そのステートメントを上に移動する必要があります。If

If dbData.HasRows Then
    While dbData.Read()
        ' ...
    End While
Else
    ' ...
End If
于 2012-10-11T18:55:57.957 に答える