从Listview检查项目更新数据库

时间:2017-07-11 16:12:40

标签: vb.net listview oledbconnection

Hellos家伙。 我正在尝试仅从列表视图中的已检查/选定项目更新我的数据库,因此这里的代码无效,因为更新了所有记录。我尝试了一切......请任何帮助都很棒!

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

3 个答案:

答案 0 :(得分:0)

我试图仅从列表视图中的已检查/选定项目更新我的数据库

如果您只想更新选择项目您必须制作(For)

   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

我希望此代码对你有好处

答案 1 :(得分:0)

解决我的问题......如果某人有相同的问题,请输入以下代码;)。

  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

答案 2 :(得分:-1)

私有子Button2_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理Button2.Click 对于x As Integer = 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