您好我正在学习如何使用 vb.net 处理 SQL 语句。我的问题是如何使用列表框中的项目作为标识符来更新我的表。我的 SQL 查询正在运行。 更新 tblBillingSched 设置 Status = 'paid' where BillNum = 'MA5'
这是修改后的代码:
Private Sub btnPostAdvancedPayment_Click(sender As Object, e As EventArgs) Handles btnPostAdvancedPayment.Click
Dim connection_string As String = "Data Source=.\sqlexpress;Initial Catalog=CreditAndCollection;Integrated Security=True"
Dim connection As New SqlConnection(connection_string)
connection.Open()
Dim SQLCmd As SqlCommand
Dim sSQL As String = "UPDATE tblBillingSched SET Status = 'Paid' WHERE BillNum = "
If MessageBox.Show("Continue to Save?", " ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
Dim firstTime As Boolean = True
For Each item In lstBillNum.Items
If (item IsNot Nothing AndAlso item.ToString().Trim().Length > 0) Then
If (firstTime) Then
firstTime = False
Else
sSQL = sSQL & " OR BillNum = "
End If
sSQL = sSQL & "'" & item.ToString() & "'"
End If
Next
SQLCmd = New SqlCommand(sSQL, Connection)
SQLCmd.ExecuteNonQuery()
MessageBox.Show("Client Record Successfully Saved!", " ", MessageBoxButtons.OK, MessageBoxIcon.Information)
connection.Close()
SQLCmd.Dispose()
ElseIf DialogResult.Cancel Then
MessageBox.Show("Saving Cancelled!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub