我有一個(家庭)表,它有兩列:名字和姓氏。第二個表(投訴)也有兩列:受訪者和評論。現在我試圖檢查(家庭)中選定的行何時與受訪者列具有相同的名稱值,并且在備注列中具有“UNSETTLED”值。然后它會顯示家庭有記錄。如果受訪者的值匹配,但備注是 = SETTLED,則該家庭沒有記錄。

凡ID在Households表的主鍵,并具有一對多的關系到HouseholdID在外鍵Complaints表。
我猜測DataGridView1包含Households并且第一列包含該ID列。
該Using塊包括連接和命令。兩者都需要處理。End Using也關閉連接。
當您在Where條件中使用文字字串時,它需要用單引號括起來。
由于您只是從資料庫中檢索單個值,因此您可以使用ExecuteScalar()它回傳一個Object,因此CInt().
一旦連接安全關閉并使用 處理End Using,您就可以檢查回傳值count,并采取所需的操作。
Private OPConStr As String = "Your connection string."
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer
Dim i = DataGridView1.CurrentRow.Index
Dim householdid = CInt(DataGridView1.Item(0, i).Value)
Try
Using con As New OleDbConnection(OPConStr),
cmd As New OleDbCommand("SELECT count(*) FROM Complaints WHERE HouseholdID = @ID AND Remarks = 'UNSETTLED';", con)
cmd.Parameters.Add("@id", OleDbType.Integer).Value = householdid
con.Open()
count = CInt(cmd.ExecuteScalar())
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
If count >= 1 Then
BrgyclearanceWithRecords.Label17.ForeColor = Color.Red
BrgyclearanceWithRecords.Label17.Text = "Resident's Name has a match with an Existing unsettled complain!"
Else
BrgyclearanceWithRecords.Label17.ForeColor = Color.Green
BrgyclearanceWithRecords.Label17.Text = "Resident's Name has no match with an Existing unsettled complain!"
End If
BrgyclearanceWithRecords.Show()
BrgyclearanceWithRecords.BringToFront()
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/351155.html
下一篇:帶有多個條件的IIfAnd陳述句
