vb.net - 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
还是没有得到我想要的结果。
解决方案
麻烦各位大佬抱歉。但这段代码确实有效。我只是没有注意到 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
推荐阅读
- angular - Angular 10 尝试将字符串解析为打字稿代码
- perl - perl使用数组对子字符串进行排序以确定整理顺序
- python - 将 np.arange 中的范围增加 1 反而将范围增加 2
- python - 如何通过密钥从 JSON 文件中获取项目?
- javascript - 尝试使用 reactjs 进行 Twitter 克隆时元素类型无效
- bash - && 在使用 GNU“并行”的 bash 脚本中
- codenameone - CN1 CameraKit 给出 null PeerComponent
- react-native - 项目垂直滚动干扰轮播水平滚动(仅限 Android)
- python - 我们如何比较两个 trie 的相似性?
- ruby-on-rails - 基于登录用户的显示元素(Ruby on Rails)