Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DataGridview和MySQL使用复选框删除行_Mysql_Vb.net_Datagridview - Fatal编程技术网

DataGridview和MySQL使用复选框删除行

DataGridview和MySQL使用复选框删除行,mysql,vb.net,datagridview,Mysql,Vb.net,Datagridview,我有一个Datagridview,我想删除MySQL数据库中的一行 我有一些代码,但我得到一个错误,它说ID是空的。My ID是一个字符串,它是检查列的ID的值。我的第一列“列0”是复选框列 代码如下: 如果你不明白我在问什么,请随时提出具体问题 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim RowsToD

我有一个Datagridview,我想删除MySQL数据库中的一行

我有一些代码,但我得到一个错误,它说ID是空的。My ID是一个字符串,它是检查列的ID的值。我的第一列“列0”是复选框列

代码如下:

如果你不明白我在问什么,请随时提出具体问题

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)        Handles Button2.Click
Dim RowsToDelete As New List(Of DataGridViewRow)




Try
For Each row As DataGridViewRow In DataGridView1.Rows

If row.Cells(0).Value = True Then

DeleteRow(row.Cells(1).Value)
RowsToDelete.Add(row)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try


For Each rowtodelete In RowsToDelete
DataGridView1.Rows.Remove(rowtodelete)

next       


End Sub

Private Sub DeleteRow(ByVal ID As Integer)
Dim MySQLCon As New MySqlConnection
Dim ConnectionString As String = "server=localhost;user id=root;password=;database=business elements"

MySQLCon.ConnectionString = ConnectionString
Dim CMD As MySqlCommand
MySQLCon.Open()
Try
CMD.Connection = MySQLCon
CMD.CommandText = "DELETE FROM `users` WHERE `ID` = " & ID

Catch ex As Exception

End Try

MySQLCon.Close()
MySQLCon.Dispose()
End Sub

在看不到所有代码的情况下,类似这样的东西应该可以工作:

Dim sql as String

sql = "DELETE FROM `users` WHERE `ID` IN ("

For Each row As DataGridViewRow In DataGridView1.Rows
    If row.Cells(0).Value = True Then
        ID = row.Cells(1).Value
        DeleteRow(row.Cells(1).Value) 
        RowsToDelete.Add(row)
        sql += ID + ","
    End If
Next

If sql.Left(sql.Length-1) = "," Then
   CMD.CommandText = sql.Left(sql.Length-1) + ")"
   CMD.Connection = MySQLCon
   CMD.ExecuteNonQuery()
End If

祝你好运。

终于找到了代码:

私有子按钮2\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮2。单击 Dim ROWS删除为新列表(DataGridViewRow的)


你能公布你的确切密码吗?在上面的代码中,您在查看ID之前设置了CommandText。此外,这很容易受到SQL注入的影响--请查看使用参数化查询的情况。是的,我在后面有它,它仍然说同样的事情。。我不明白你在说什么,你能解释一下吗?看看我的答案,看看是否有用。不使用参数化查询,但会比运行多个deletes.BTW更有效--这是假设您的ID在For循环中设置正确…Left不是string的成员?@kraxe--可能取决于您的导入(我主要使用C#)--尝试以下操作:sql.Substring(0,sql.Length-1)@kraxe--或者查看此URL@kraxe,您可以使用LEFT(sql,LEN(sql)-1--我认为这也应该可以:)
    Try
        For Each row As DataGridViewRow In DataGridView1.Rows

            If row.Cells(0).Value = True Then

                DeleteRow(row.Cells(1).Value)
                RowsToDelete.Add(row)
            End If
        Next
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try


    For Each rowtodelete In RowsToDelete
        DataGridView1.Rows.Remove(rowtodelete)

    Next



End Sub

Private Sub DeleteRow(ByVal ID As Integer)
    Dim MySQLCon As New MySqlConnection
    Dim ConnectionString As String = "server=localhost;user id=root;password=;database=business elements"
    MySQLCon.ConnectionString = ConnectionString
    Dim CMD As New MySqlCommand
    CMD.CommandText = "DELETE FROM `users` WHERE `ID` = " & ID
    MySQLCon.Open()
    Try
        CMD.Connection = MySQLCon

        CMD.ExecuteNonQuery()

    Catch ex As Exception

    End Try







    MySQLCon.Close()
    MySQLCon.Dispose()
End Sub