0

Datagridview があり、MySQL データベースの行を削除したいと考えています。

いくつかのコードがありますが、ID が null であるというエラーが表示されます。私の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

4

2 に答える 2

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 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
于 2013-02-12T00:13:18.187 に答える
0

すべてのコードを表示しなくても、次のようなものが機能するはずです。

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

幸運を。

于 2013-02-11T23:43:31.880 に答える