2013-03-27 107 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(我不确定它将在哪个单元格中,但是您应该能够从您的代码中知道)。