首页 > 解决方案 > For 循环 继续 For

问题描述

我正在尝试过滤列表视图中列出的项目。如果列表视图上的每个项目已经存在于我的数据库中,则应检查它。那些不在数据库中的,将被插入。

这是我尝试过的代码。

Sub Save()
    For Each item As ListViewItem In ListViewSelectedOffice.Items
        CONNECTION.Close()
        CONNECTION.Open()
        Query = "SELECT * FROM table_documentdestinationlog WHERE LogNo = '" & Me.TextBoxLogNo.Text & "' AND OfficeDestination = '" & item.SubItems(1).Text & "'"
        comm = New MySqlCommand(Query, CONNECTION)
        Reader = comm.ExecuteReader
        If Reader.HasRows = True Then
            Continue For
        End If
        'Do Something
        CONNECTION.Close()
        CONNECTION.Open()
        Query = "INSERT INTO table_documentdestinationlog (LogNo, OfficeDestination, Status) VALUES (@logno, @officeD, 'Pending')"
        comm = New MySqlCommand(Query.ToString, CONNECTION)
        comm.Parameters.AddWithValue("@logno", LabelLogNo.Text)
        comm.Parameters.AddWithValue("@officeD", item.SubItems(1).Text)
        comm.ExecuteNonQuery()
    Next item

我也试过

 Sub SaveToDestinationLog1()
        For Each item As ListViewItem In ListViewSelectedOffice.Items
            CONNECTION.Close()
            CONNECTION.Open()
            Query = "SELECT * FROM table_documentdestinationlog WHERE LogNo = '" & Me.TextBoxLogNo.Text & "' AND OfficeDestination = '" & item.SubItems(1).Text & "'"
            comm = New MySqlCommand(Query, CONNECTION)
            Reader = comm.ExecuteReader
            If Reader.HasRows = True Then
                Continue For

            Else
                GoTo LINE1
            End If

LINE1:
            'Do Something
            CONNECTION.Close()
            CONNECTION.Open()
            Query = "INSERT INTO table_documentdestinationlog (LogNo, OfficeDestination, Status) VALUES (@logno, @officeD, 'Pending')"
            comm = New MySqlCommand(Query.ToString, CONNECTION)
            comm.Parameters.AddWithValue("@logno", LabelLogNo.Text)
            comm.Parameters.AddWithValue("@officeD", item.SubItems(1).Text)
            comm.ExecuteNonQuery()
        Next item
    End Sub

还是没有得到我想要的结果。

标签: vb.net

解决方案


麻烦各位大佬抱歉。但这段代码确实有效。我只是没有注意到 WHERE 子句的值是错误的。我没有注意到我在 WHERE 子句中使用了 TextBoxLogNo.text 而不是 LabelLogno.text。这就是为什么它没有为 reader.hasrows 返回正确的值。我为每次迭代都添加了 msgbox,并注意到它实际上并没有返回我想要的值。所以我查看了我的 Query 和 WHERE 子句。当然,您的所有建议都非常有帮助。

Sub Save()
    For Each item As ListViewItem In ListViewSelectedOffice.Items
        CONNECTION.Close()
        CONNECTION.Open()
        Query = "SELECT * FROM table_documentdestinationlog WHERE LogNo = '" & Me.TextBoxLogNo.Text & "' AND OfficeDestination = '" & item.SubItems(1).Text & "'"
        comm = New MySqlCommand(Query, CONNECTION)
        Reader = comm.ExecuteReader
        If Reader.HasRows = True Then
            Continue For
        End If
        'Do Something
        CONNECTION.Close()
        CONNECTION.Open()
        Query = "INSERT INTO table_documentdestinationlog (LogNo, OfficeDestination, Status) VALUES (@logno, @officeD, 'Pending')"
        comm = New MySqlCommand(Query.ToString, CONNECTION)
        comm.Parameters.AddWithValue("@logno", LabelLogNo.Text)
        comm.Parameters.AddWithValue("@officeD", item.SubItems(1).Text)
        comm.ExecuteNonQuery()
    Next item

推荐阅读