从列表视图中更新数据库选中的项目

UPDATE Database from Listview checked items

大家好。 我正在尝试仅从列表视图中的 checked/selected 项更新我的数据库,因此我的代码无法正常工作,因为更新了所有记录。我尝试了一切...请提供任何帮助!

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\bnp\Desktop\Servicios1.mdb"
    Dim myconnection As New OleDbConnection(constring)

    Dim sql As String = ""
    Dim estado As String = "Saved"
    myconnection.Open()

    If ListView1.CheckedItems.Count > 0 Then

        Dim x = ListView1.CheckedItems.ToString

        sql = "UPDATE Pedidos SET Ped_est = @estado where x = @x"

        Dim acscmd = New OleDb.OleDbCommand(sql, myconnection)
        acscmd.Parameters.AddWithValue("@estado", estado)
        acscmd.Parameters.AddWithValue("@x", x)
        acscmd.ExecuteNonQuery()

    End If
    myconnection.Close()
End Sub

我正在尝试从我的列表视图中的 checked/selected 个项目更新我的数据库

如果您只想更新 Select 项您必须制作(针对)

   For II As Integer = 0 To ListView1.SelectedItems.Count - 1
            sql = "UPDATE Pedidos SET Ped_est = @estado where x 
          = '" & ListView1.SelectedItems.Item(II).SubItems(0).Text & "'"

   your code Here 

   Next

我希望这个代码对你有用

只要解决我的问题...如果有人有同样的问题,这里是代码;)。

  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\bnp\Desktop\Servicios1.mdb"
    Dim myconnection As New OleDbConnection(constring)

    Dim sql As String = ""
    Dim estado As String = "Saved"

    myconnection.Open()

    For Each item As ListViewItem In ListView1.Items
        If item.Checked Then
            Dim x = item.SubItems(0).Text
            sql = "UPDATE Pedidos SET Ped_est = @estado WHERE Id = @x"

            Dim acscmd = New OleDb.OleDbCommand(sql, myconnection)
            acscmd.Parameters.AddWithValue("@estado", estado)
            acscmd.Parameters.AddWithValue("@x", x)
            acscmd.ExecuteNonQuery()
        End If
    Next
    myconnection.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 对于 x 作为整数 = 0 至 ListView1.Items.Count - 1

        ' For i = 0 To ListView1.Items.Count - 1


        If ListView1.Items(x).Checked = True Then
            Dim Stradd12 As String = "INSERT INTO `tbl_student_attendence` ([student_id],[Student_Name],[Activities],[pDate],[pDay],[Ptime]) VALUES (student_id,Student_Name,Activities,pDate,pDay,Ptime )"
            'Dim Stradd12 As String = "INSERT INTO `tbl_student_attendence` ([student_id]) VALUES (student_id )"

            Dim cm12 As OleDbCommand = New OleDbCommand(Stradd12, DBconnection)
            cm12.Parameters.AddWithValue("student_id", CType(ListView1.Items(x).SubItems(0).Text, Integer))
            cm12.Parameters.AddWithValue("Student_Name", CType(ListView1.Items(x).SubItems(1).Text, String))
            cm12.Parameters.AddWithValue("Activities", CType(ListView1.Items(x).SubItems(2).Text, String))
            cm12.Parameters.AddWithValue("pDate", CType(DateTimePicker1.Value, String))
            cm12.Parameters.AddWithValue("pDay", CType(ComboBox1.Text, String))
            cm12.Parameters.AddWithValue("Ptime", CType(ListView1.Items(x).SubItems(3).Text, String))
            'cm12.Parameters.AddWithValue("Registration_no", CType(TextBox1.Text, Integer))
            cm12.ExecuteNonQuery()
            cm12.Parameters.Clear()
        End If
        ' Next
    Next
    MsgBox("Attendence Updated Successfully.")
End Sub