2013-03-27 126 views
1

我有這個功能工作正確。正確工作的部分是我可以在DataGridView上選擇一行的位置,使用「Delete Row」按鈕調用此函數,然後它將從DataGridView中刪除該行....但是,它不會刪除行在數據庫上。使用OleDb從數據庫刪除行

任何人都可以幫我從數據庫中刪除行使用OleDb?

Function DeleteTableRow() 
    Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database") 
    Dim dbConnection = New OleDbConnection(TaxConnStr) 

    Try 
     Dim dbCommand As OleDbCommand = New OleDbCommand 
     Dim rdr2 As OleDbDataReader 

     Dim selectedRow = DataGridView1.SelectedRows 

     dbCommand.CommandText = "DELETE FROM UserCriteria WHERE RowID =" & selectedRow 
     If dbConnection.State = ConnectionState.Closed Then 
      dbConnection.Open() 
     End If 

     dbCommand.Connection = dbConnection 
     rdr2 = dbCommand.ExecuteReader 
     dbCommand.ExecuteNonQuery() 


     rdr2.Close() 

     '''Must select entire row to delete 
     'DataGridView1.Rows.Remove(DataGridView1.Rows(DataGridView1.SelectedCells.Item(0).RowIndex)) 

     '''allows you to select on cell in the row to delete entire row 
     For Each oneCell As DataGridViewCell In DataGridView1.SelectedCells 
      If oneCell.Selected Then 
       DataGridView1.Rows.RemoveAt(oneCell.RowIndex) 
      End If 
     Next 



    Catch ex As Exception 
     MsgBox(ex.Message) 
    Finally 
     dbConnection.Close() 
    End Try 
End Function 

回答

2

DataGridView.SelectedRowsDataGridViewRow集合,你不能用一個集合作爲參數來刪除數據庫表中特定的和具體的記錄。 (你有OPTION STRICT set tot OFF?)

您在收集需要循環,得到每一行的正確ID值並使用該值作爲參數傳遞給你刪除查詢。

If dbConnection.State = ConnectionState.Closed Then 
    dbConnection.Open() 
End If 

' Creating the command and its parameter here before entering the loop to avoid a continue' 
' create and destroy pattern for the OleDbCommand' 
Dim dbCommand As OleDbCommand = New OleDbCommand 
dbCommand.CommandText = "DELETE FROM UserCriteria WHERE ID =?" 
dbCommand.Connection = dbConnection 
dbCommand.Parameters.AddWithValue("@row", 0) 
Dim rows = DataGridView1.SelectedRows 
For Each row in rows 
    dbCommand.Parameters("@row").Value = row.Cells("ID").Value) 
    dbCommand.Connection = dbConnection 
    dbCommand.ExecuteNonQuery() 
Next 

還要注意不要使用字符串連接來構建sql命令。這種習慣導致了一種稱爲Sql注入的蠕蟲整體罐

當然,這裏不需要OleDbDataReader。 (沒有閱讀有關)

+0

更正---數據庫中的列名稱就是「ID」。另外,在DataGridView中,我將此列(「ID」列)設置爲隱藏。這是一個問題嗎? – MaylorTaylor 2013-03-27 17:16:19

+0

不,只是改變代碼來反映這一事實。我將更新答案 – Steve 2013-03-27 17:17:44

+0

您的For循環似乎不正確。我有「對於Datagridview1.selectedrows中的每個selectedRow」... ... – MaylorTaylor 2013-03-27 17:21:36

1

您不需要讀者刪除一行。沒有數據將被退回

rdr2 = dbCommand.ExecuteReader 
    dbCommand.ExecuteNonQuery() 


    rdr2.Close() 

應該簡單地

dbCommand.ExecuteNonQuery() 
0

的問題是,你DataGridView1.SelectedRows將返回SelectedRowCollection(對不起,我所做的假設,這是一個WinForms應用程序)。兼)SelectedRowCollection的傳遞給你CommandText時候,因爲你可能會得到的ToString(而不是你後

你真正想要做的是遍歷所有的ID,這不會讓你正確的結果集合(如果用戶能夠選擇多行),並刪除了所選的每一行,是這樣的:

For Each selectedRow in DataGridView1.SelectedRows 
    '1. Get the DatabaseId of the selected row 
    '2. Modify dbCommand.CommandText to use the selected row from 1 
    '3. execute command like you are doing with ExecuteNonQuery 
Next 

每個selectedRow在上面會this型的......其中有一個Cells您可以訪問的屬性以獲取您需要的ID(我不確定它將在哪個單元格中,但是您應該能夠從您的代碼中知道)。