您好我正在学习如何使用vb.net处理SQL语句。我的问题是如何使用列表框中的项目作为标识符更新我的表。 我的SQL查询正在运行。 update tblBillingSched set Status ='paid' 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
答案 0 :(得分:1)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim dt as date = CDate(DateTime.Today).ToString("MM/dd/yyyy")
Try
Dim cn As New OleDb.OleDbConnection
cn.ConnectionString = GetConnectionStringByName("FD_project.My.MySettings.fixedepoConnectionString")
Dim sSQL As String = "UPDATE fixdeporeg SET approval = 'A', a_user = 'CFO', " & _
"app_date = # " & dt & "# WHERE slno = "
If MessageBox.Show("Continue to Save?", " ", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
Dim firstTime As Boolean = True
For x As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
If (firstTime) Then
firstTime = False
Else
sSQL = sSQL & " OR slno = "
End If
'sSQL = sSQL & "'" & item.ToString() & "'"
sSQL = sSQL & CheckedListBox1.CheckedItems(x).item("slno")
Next
Dim SQLCmd As OleDbCommand = New OleDbCommand(sSQL, cn)
MessageBox.Show(sSQL)
cn.Open()
'dt = cmd1.ExecuteScalar()
SQLCmd.ExecuteNonQuery()
cn.Close()
ElseIf DialogResult.Cancel Then
MessageBox.Show("Saving Cancelled!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
答案 1 :(得分:0)
我知道你在这些行上循环:
Dim sSQL As String = "UPDATE tblBillingSched SET Status = 'paid' WHERE BillNum = "
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