我希望能够点击我的datagridview中的一条记录,该记录选择了tbl_requested-shifts
中的所有记录,当点击一个按钮时,此记录将从tbl_requested_shifts
中移除并移至{{1}任何人都可以帮我完成这项任务的SQL吗?
顺便说一句,我在vb.net 2010中编码
答案 0 :(得分:0)
我还没有测试过,但我认为它会起作用。
每当在DataGridView中选择一行时,从中获取主键列值。我们假设第1列是主键列,它是整数类型。
我把代码放在了 CellClick 事件中。您可以使用 CellContentClick 事件,但我发现 CellClick 更可靠。确保 DataGridView 的 MultiSelect 属性设置为 false
Dim value as Integer
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Try
value = DataGridView1.Rows(e.RowIndex).Cells(0).Value
Catch ex As Exception
End Try
End Sub
现在,在Button的点击事件中。有一个查询组,如下所示。
INSERT INTO tbl_shifts (col1, col2,col3,col4)
SELECT col1,col2,col3,col4
FROM tbl_requested_shifts
WHERE col1 =value;
DELETE FROM tbl_requested_shifts
WHERE col1=value;
试试吧。感谢。
其他强>
objCommand = New SqlCommand
objCommand.CommandText = "INSERT INTO tbl_shifts (col1, col2,col3,col4) " & _
"SELECT col1,col2,col3,col4 " & _
"FROM tbl_requested_shifts " & _
"WHERE col1 =" & value & ";" & _
"DELETE FROM tbl_requested_shifts " & _
"WHERE col1=" & value & ";"
Try
<your sql connection object>.Open()
command.Connection = <your sql connection object>
command.ExecuteNonQuery()
<your sql connection object>.Close()
Catch ex As Exception
MsgBox(ex.Message)
<your sql connection object>.Close()
End Try
以上 T-SQL语句将在一个表中添加行,并从其他表中删除行。并确保在执行此代码后重新加载 datagridview 。感谢。
答案 1 :(得分:0)
为将记录从表1删除到表2中,将记录从表1删除到表2中,然后在button_click事件中调用该函数,如下所示。希望这会起作用...
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Try
Call Connect()
STRSQL = "Insert into table2 select * from table1 where" & _
"id=" & TextBox1.Text & ""
myCmd = New OleDbCommand(STRSQL, myConn)
myCmd.ExecuteNonQuery()
myConn.Close()
Deletedata()
Catch ex As Exception
End Try
End Sub
Private Sub Deletedata()
Call Connect()
STRSQL = "delete from table1 where ID=" & TextBox1.Text & ""
myCmd = New OleDbCommand(STRSQL, myConn)
'myConn.Open()
Dim da As New OleDbDataAdapter(myCmd)
Dim dt As New DataTable
Try
da.Fill(dt)
myCmd.ExecuteNonQuery()
myConn.Close()
Catch ex As Exception
End Try
End Sub