0

您好我正在学习如何使用 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
4

2 回答 2

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
于 2014-03-10T02:49:01.977 回答
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
于 2013-09-19T10:54:40.333 回答