【发布时间】:2015-04-20 15:17:09
【问题描述】:
我在删除 SQL 数据库行以供下次删除时遇到问题。 第一次,它工作正常,但第二次,我收到错误消息(命令参数 [1] 无效)。
我使用 datagridview 来选择我想要删除的行(单/多)。 这是我的代码:
Dim response As MsgBoxResult
Dim i As Integer
i = DataGridView1.CurrentRow.Index
If Not Trim(DataGridView1.Item("Group", i).Value.ToString) = "Admin" Then
response = MsgBox("Do you want remove this account ?" & vbNewLine & "Account : " & Trim(DataGridView1.Item("Name", i).Value.ToString), _
MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Delete Data")
If response = MsgBoxResult.Yes Then
If Not MyConnection.State = ConnectionState.Open Then
MyConnection.Open()
End If
cmd.Connection = MyConnection
cmd.CommandText = "DELETE FROM t_User WHERE ID =?"
cmd.Parameters.AddWithValue("@row", 0)
For Each UserRow As DataGridViewRow In DataGridView1.SelectedRows
If Not MyConnection.State = ConnectionState.Open Then
MyConnection.Open()
End If
cmd.Connection = MyConnection
cmd.Parameters("@row").Value = UserRow.Cells("ID").Value
DataGridView1.Rows.Remove(UserRow)
cmd.ExecuteNonQuery()
DeleteColumns()
CreateColumns()
MyConnection.Close()
Next UserRow
Me.BackgroundWorker1.RunWorkerAsync()
End If
End If
请帮助我,非常感谢您的帮助
【问题讨论】:
标签: sql-server vb.net visual-studio-2010 visual-studio-2012 vbscript