2013-02-11 74 views
0

我有一個Datagridview,我想刪除我的MySQL數據庫中的一行。DataGridview和MySQL使用複選框刪除行

我有一些代碼,但我得到一個錯誤,它說ID爲空。我的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 

+0

你可以發佈你的確切代碼?在上面的代碼中,您在設置您的ID之前設置了您的CommandText。另外,這很容易受到SQL注入的影響 - 使用參數化查詢。 – sgeddes 2013-02-11 23:30:16

+0

是的,我有它後,它仍然說同樣的事情..我不明白你在說什麼,你能解釋.. – Kraxed 2013-02-11 23:44:09

+0

看到我的答案,看看是否沒有幫助。不使用參數化查詢,但會比運行多個刪除更有效。 – sgeddes 2013-02-11 23:45:56

回答

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 

好運。

+0

左邊不是字符串的成員? – Kraxed 2013-02-11 23:50:16

+0

@Kraxed - 可能取決於你的Imports(我主要使用C#) - 試試這個:sql.Substring(0,sql.Length-1) – sgeddes 2013-02-11 23:55:25

+0

@Kraxed - 看到這個URL http://msdn.microsoft.com /en-us/library/microsoft.visualbasic.strings.left(v=vs.100).aspx – sgeddes 2013-02-11 23:56:30

0

最後找到的代碼:

私人小組Button2_Click(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)把手Button2.Click 昏暗RowsToDelete作爲新的列表(中的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