Vb.net 我的sub中的某些代码无法执行

Vb.net 我的sub中的某些代码无法执行,vb.net,oledb,oledbdatareader,Vb.net,Oledb,Oledbdatareader,我的程序中有一个子系统,如果项目过期,它将从access数据库中删除项目。我正在使用oledbdatareader查找需要删除的行。我的问题是,这个子系统的大部分将不会执行。我使用messagebox告诉我,在定义了datareader之后,代码不会运行 我已经在许多其他函数和表单中使用了相同的代码,它工作得很好,但由于某些原因,在这个sub中没有。有人能看到我的问题吗 Private Sub AutoDate() Using connection As New OleDbConnect

我的程序中有一个子系统,如果项目过期,它将从access数据库中删除项目。我正在使用oledbdatareader查找需要删除的行。我的问题是,这个子系统的大部分将不会执行。我使用messagebox告诉我,在定义了datareader之后,代码不会运行

我已经在许多其他函数和表单中使用了相同的代码,它工作得很好,但由于某些原因,在这个sub中没有。有人能看到我的问题吗

Private Sub AutoDate()
    Using connection As New OleDbConnection(connectionstring)
        connection.Open()

        Dim Command As New OleDbCommand("SELECT ExpirationDate FROM Iventory", connection)
        Dim Command2 As New OleDbCommand("DELETE FROM Inventory WHERE ExpirationDate = @p1", connection)

        MessageBox.Show("Got here") 'This messagebox shows
        Dim Reader As OleDbDataReader = Command.ExecuteReader()
        MessageBox.Show("Got here") 'This messagebox does not show

        While Reader.Read()
                Dim ExpDate As DateTime = Reader.Item("ExpirationDate")
                Command2.Parameters.AddWithValue("@p1", ExpDate)

                If ExpDate.ToString < System.DateTime.Today.ToString Then
                    Dim cmd = Command2.ExecuteNonQuery()
                    If cmd > 0 Then
                        MessageBox.Show("Out of date items have been removed from database")
                    Else
                        Exit Sub
                    End If
                End If
        End While
        connection.Close()
    End Using
End Sub

删除命令对象。您正在为此调用execute reader,因此它不会删除任何内容。恐怕我不确定您的意思。你能详细说明一下吗?第一个SQL SELECT语句是不完整的;不确定它的用途是什么,如果您要删除过期的内容,则第一条语句将读取库存表中的所有过期日期,以便我可以检查它是否在今天的日期之前,您不需要读取所有过期日期。您的DELETE已经检查它是否在WHERE子句指定的日期之前。只需将今天的日期作为参数传递给Command2。为了使它更干净,您可以将=更改为