从列表视图检查项更新数据库



大家好。 我正在尝试仅从列表视图中选中/选定的项目更新我的数据库,所以这是我的代码不起作用,因为更新了所有记录。我什么都试过了...请任何帮助都很棒!

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:UsersbnpDesktopServicios1.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

我正在尝试仅从列表视图中选中/选定的项目更新我的数据库

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

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:UsersbnpDesktopServicios1.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( 句柄按钮2.Click 对于 x 作为整数 = 0 到列表视图 1.项目.计数 - 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

相关内容

  • 没有找到相关文章

最新更新